While using PMD code analyser,
I have shown several duplicates which includes framework classes and delegates and datasource methods too, I just want to suppress those findings. I tried with Suppress warnings not works. Also I can't find syntax for Objective-c. Can any one give me how to suppress duplicate findings in PMD?
I tried the below command for excluding the directory,
./run.sh cpd --files
/Users/Arun/Documents/Projects/Sample
--language objectivec --minimum-tokens 100 --format xml --exclude /Users/Arun/Documents/Projects/Sample/ExternalFrameworks.
I don't see any duplicate suppression syntax for objective-c.
There is currently no support to have CPD ignore chunks of code in any other language than Java. The only option available is to fully ignore files using the --exclude flag.
Supporting it through comments (// CPD-[OFF|ON] | /* CPD-[OFF|ON] */) should be relatively easy since Objective-C is implemented using JavaCC.
The source code is tokenized here. CPD suppression consists merely of dropping the ignored tokens from tokenEntries.
To do so, you first need to find comments, this is done by checking if currentToken.specialToken is not null.
Token st = currentToken.specialToken;
while (st != null) {
if (st.image.contains("CPD-OFF") {
suppressing = true;
break;
}
if (st.image.contains("CPD-ON") {
suppressing = false;
break;
}
st = st.specialToken;
}
and then checking if the value of suppressing before adding (or not) the token here
Final code should look something such as:
boolean suppressing = false;
while (currentToken.image.length() > 0) {
if (!suppressing) {
tokenEntries.add(new TokenEntry(currentToken.image, sourceCode.getFileName(), currentToken.beginLine));
}
currentToken = (Token) tokenManager.getNextToken();
Token st = currentToken.specialToken;
while (st != null) {
if (st.image.contains("CPD-OFF") {
suppressing = true;
break;
}
if (st.image.contains("CPD-ON") {
suppressing = false;
break;
}
st = st.specialToken;
}
}
PRs are always welcomed. This along with a couple unit tests should be enough to get this merged for the next release.
Related
I am using stm32H753xx and Cube FatFs library.
After our e-MMC has been soldered to our board, I want to bring-up.
My startup codes is here:
res = f_mount(&fat_fs, "0:", 0);
if (res != FR_OK) {
res = f_mkfs("", FM_ANY, 0, work, sizeof(work));
if (res != FR_OK) {
while(1) {
LOGGER_REPORT(FORMAT_REQUEST_FAILED);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
res = f_mount(&fat_fs, "0:", 0);
if (res != FR_OK) {
while(1) {
LOGGER_REPORT(FORMATTED_BUT_NOT_MOUNTED);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
}
I generally expect an error at mounting phase if bringing-up of a memory device is very first time and this implies why I made my software branch to f_mkfs functions if f_mount fails. But f_mount is returning FR_OK and software is skipping here.
Afterwards, I am doing some api calls to detect latest directory in the root and to create new one by giving a name in way that would be latest+1. ( latest is like ./70/ new one ./71/ anyway)
There are some api calls here f_opendir, f_readdir, f_closedir respectively and all of them returns succesfully but,
whenever I want to create new dir by calling fs_mkdir, it returns FR_NO_FILESYTEM.
If I call f_mkfs after f_mount above, a FAT is creating and software works but I can not call f_mkfs ile that and could not figure it out where I have to put this code to make it run only once at very initial bring-up of e-MMC.
I want to create an output file in a threadsafe manner, and only if it does not exist. I want to use the file system for synchronization. With open() I would use the flags O_RWRONLY|O_CREAT|O_EXCL. Is there a way to do this in C++17 using the iostream or fstream ?
Prior to C++23 there is no way of opening an ofstream in exclusive mode.
Workaround: Use std::fopen which has this capability since C++17.
Example:
#include <cstdio>
// Mode "x" to make it fail if it already exists
std::FILE* fp = std::fopen("filename", "wx");
if(fp) {
// created exclusively
// work with fp ...
std::fclose(fp);
}
If you really want an ofstream you could create a helper function:
template<class Stream>
Stream open_exclusively(const std::string& filename) {
Stream rv;
if(std::FILE* fp = std::fopen(filename.c_str(), "wx"); fp) {
std::fclose(fp);
// overwrite the file that was created exclusively:
rv.open(filename);
} else {
// could not create file exclusivly, set the failbit in the stream:
rv.setstate(Stream::failbit);
}
return rv;
}
int main() {
auto os = open_exclusively<std::ofstream>("filename");
if(os) {
std::cout << "file created exclusively\n";
}
}
Demo
Edit:
Even though the above demo is compliant and works on all platforms I've tested it - wine (v6.16) can't handle it, so I opened a bug report at bugs.winehq.org. You can follow the progress here:
Standard library call fopen(..., "wx") not recognized - causes destruction of data
Edit 2:
The Wine bugfix ucrtbase: Add support for x mode in fopen is now included in Wine 6.20 so after upgrading to 6.20 (or later), this will be working as it should in Wine too.
From C++23 you can use the std::ios::noreplace openmode:
std::ofstream os("filename", std::ios::noreplace);
if(os) {
std::cout << "file created exclusively\n";
}
I have the following code, for which address sanitizer only catches the violation when LTO is off. Changing between -Os and -O0 doesn't affect it. Any ideas why?
char *__attribute((noinline)) SCObfuscatedMalloc();
void SCCauseAddressSanitizerViolation() {
char *chars = SCObfuscatedMalloc();
if (rand() & 1) {
chars[2] = 3;
} else {
chars[2] = 2;
}
printf("yo: %zd\n", (NSInteger)chars[2]);
}
char *__attribute((noinline)) SCObfuscatedMalloc() {
return malloc(1);
}
I recently ran into this problem, not on clang bug GCC though. I looked at the objdump with and without -flto: it appears LTO removed all calls to the instrumented checker functions.
I guess what LTO did is: it looked at the inserted check code and found that these code can never be reachable, because they are checking UBs, which LTO assumes can never happen. So LTO just removed them.
But O3 was good.
In Jest, is there any way to ignore code for test coverage?
I tried using
/* istanbul ignore next */
But it doesn't seem to work.
It works.
(function(global) {
var defineAsGlobal = true;
/* istanbul ignore next */
if(typeof exports === 'object') {
module.exports = lib;
defineAsGlobal = false;
}
/* istanbul ignore next */
if(typeof modules === 'object' && typeof modules.define === 'function') {
modules.define('lib', function(provide) {
provide(lib);
});
defineAsGlobal = false;
}
/* istanbul ignore next */
if(typeof define === 'function') {
define(function(require, exports, module) {
module.exports = lib;
});
defineAsGlobal = false;
}
/* istanbul ignore next */
defineAsGlobal && (global.lib = lib);
})(this);
Sample project https://github.com/ilyar/sandbox/tree/master/jest
Update for anyone that finds this at a later date.
/* istanbul ignore next */
Will work but as read from The Jest Official Documentation:
coveragePathIgnorePatterns seems to not have any effect.
Make sure you are not using the babel-plugin-istanbul plugin. Jest
wraps Istanbul, and therefore also tells Istanbul what files to
instrument with coverage collection. When using babel-plugin-istanbul,
every file that is processed by Babel will have coverage collection
code, hence it is not being ignored by coveragePathIgnorePatterns.
The documentation can be found here: Documentation
So in order to fix this issue uninstall babel-plugin-istanbul:
If it is a library based only on javascript, than you can just run npm uninstall --save babel-plugin-istanbul or npm uninstall --save-dev babel-plugin-istanbul
If you've installed a library with native content that requires linking, and you've linked it with rnpm then you can do: rnpm unlink package_name then follow step 1 - Aakash Sigdel
This quote was from Aakash Sigdel found here: quote
Found a workaround (spaces before and after the comment seem to be necessary):
class Foo {
bar /* istanbul ignore next */ () {
return 'biu~';
}
}
Just for anyone finding this who uses the v8 provider:
the docs now state how to ignore lines with different providers, and link to more details. But basically either /* c8 ignore next */ or /* c8 ignore start */ + /*c8 ignore end */ should work well with the v8 provider.
Example:
/* c8 ignore next */
if (process.env.DEBUG) console.log('debug');
/* c8 ignore start */
switch (process.env.DEBUG) {
case '1':
console.log('some verbosity');
break;
case '2':
console.log('a lot of verbosity');
break;
}
/* c8 ignore end */
In my case, I had coverageProvider: 'v8', and it was causing this to break. doing: coverageProvider: 'babel', fixed it and the pragma works great.
According to a babel issue thread Istambul appears to have a bug where it assumes the preceding line of code is terminated with a semi-colon...
constructor(message: string) {
// TODO: how do I get Jest code coverage for "super(message)?
// /* istanbul ignore next */ assumes the preceding line of code is terminated with a ;
DEBUG: console.log();
/* istanbul ignore next */
super(message);
}
I have a service in my application which return a list of tracks, here is the code for that
List<Track> getTrackListTracks(String listName, int max) {
def tracks = getTrackListTracks(listName)
if(tracks?.size() > max) {
tracks = tracks[0 ..< max]
}
return tracks
}
List<Track> getTrackListTracks(String listName) {
def tl = TrackList.findByName(listName)
if(tl?.tracks) {
return tl?.tracks?.collect { Track.read(it.trackId) }
}
}
i have to write the unit test for this but I am not able to write. Can anyone help me in this.
Thanks Already
Hopefully you've progressed beyond this, but for those coming after, the grails-spock-examples project # google code (https://github.com/pschneider-manzell/grails-spock-examples) has a wide range of examples.
More specifically, for a service (as you've asked), check out Testing Services.
Caution, though - there are a few differences between that and what is required for Grails 2. For example, if testing controllers, 'redirectArgs' is no longer valid. Make sure to also consult the Grails Documentation for differences.