I need a trailing comment in my code. Sonar keeps throwing this error. Unlike the public version, our Sonar says there is a way around this...
Parameters
legalCommentPattern
Pattern for text of trailing comments that are allowed.
Default Value:
^//\s*+[^\s]++$
This seems to suggest there is a way to add a comment to the EOL (which I need to do) but playing around in Regex pal I can't seem to figure it out.
How do I add a trailing comment that meets the legalCommentPattern. Or how do I change the value to allow for inline shorter comments.
Related
I am trying to introduce clang-format to a couple of our projects at work (C and C++), but I am having trouble getting it to format multi-line Doxygen comments the way I want.
All comments have the same format:
/*! #brief Some text
*
* Some more text
*
* #verbatim
*
* A very long line of text that exceeds the clang-format column width but should not be touched
*
* #endverbatim
*/
I want clang-format to leave the verbatim blocks alone and not reflow them. I am using clang-format-6.0
Turning ReflowComments off is not an option as non-doxygen comments must be taken care of by clang-format
I have tried various regular expressions in the CommentPragmas config item but to no avail:
#verbatim(.*\n)*.*#endverbatim to treat the entire verbatim block as a comment pragma. This is the ideal situation, as any other part of the Doxygen comment I do not mind being broken into multiple lines.
#brief(.*\n)+ to match the entire comment block as the pragma. I've also tried this with an arbitrary token at the end of the comment to act as an explicit end-of-block marker. This isn't ideal as it doesn't force the non-verbatim part of the comment to conform, but is a compromise I'm willing to live with if I have to.
Various other regexes I've seen in other discussions, adapted to fit our Doxygen markup.
All I've managed to get it to do so far is to leave the first line of the multi-line comment alone, if it happens to exceed the column limit. However, any following long line is still broken up.
The only other tool I have left in my box is to use // clang-format off and // clang-format on around these comments but again I'd like to avoid it if I can because:
a) it'll be quite tedious to add them throughout the code base
b) I'll have to surround the entire comments with these, rather than just the verbatim blocks (I haven't figured out if you can disable it just for a portion of a multi-line comment - I've only managed to get it working for an entire one, and even if that was possible the clang-format directives would end up in the generated Doxygen docs which is unacceptable)
c) I don't really like the way it looks in code.
Any help is appreciated. Thanks.
Ran into this issue also, and the only work around found was to use clang-format on/off.
clang-format re flowing comments tends to:
break #page, #section, etc titles, and links generated from them (in rare cases).
break #startuml blocks, which have a specific syntax.
break #verbatim blocks.
See an example of usage in MySQL:
https://github.com/mysql/mysql-server/blob/8.0/storage/perfschema/pfs.cc
Update:
Filed a feature request on clang-format itself:
https://bugs.llvm.org/show_bug.cgi?id=44486
I followed the instructions in question
Ignore Issues in Blocks
but since it didn't work, I suspect it may be something related with the regex rather than the Sonar configurations.
I have some code that is generated by EMF. I changed the code generation templates to also add //end-generated tag at the end of block, which looks like this:
/*
* #generated
*/
public class MyGeneratedClass implements Enumerator {
/*
* #generated
*/
public void myGeneratedMethod() {
// some code that SonarQube doesn't like
} //end-generated
}
The idea is that the regex must only match method blocks. So the START-BLOCK must match text #generated as long it's not followed by 'class', 'enum' or 'interface' word after the comment termination.
I created this regex:
For start block:
#generated\h*\s[\s\*]*\/(?!\s*[^\s]*\s*(class|enum|interface)\s)
For end block:
\/\/end-generated
This works in my tests using a simple code with Java Pattern class, which returns true for class sample above:
public static void main(String[] args) throws IOException {
String regex = "#generated\\h*\\s[\\s\\*]*\\/(?!\\s*[^\\s]*\\s*(class|enum|interface)\\s)";
String text = new String(Files.readAllBytes(Paths.get("test.txt")));
Matcher matcher = Pattern.compile(regex).matcher(text);
System.out.println(matcher.find());
}
However when I add it to SonarQube configurations, it doesn't work, issues found in the generated method are still reported by SonarQube.
I added the regex in:
SonarQube » Settings » Exclusions » Issues » Ignore Issues in Blocks
I also tried with escaped version of regex, and result is the same:
#generated\\h*\\s[\\s\\*]*\\/(?!\\s*[^\\s]*\\s*(class|enum|interface)\\s)
Also adding these settings directly into Sonar properties didn't work, as reported already in the question I mentioned above:
sonar.issue.ignore.block=rule1
sonar.issue.ignore.block.rule1.beginBlockRegexp=#generated\\h*\\s[\\s\\*]*\\/(?!\\s*[^\\s]*\\s*(class|enum|interface)\\s)
sonar.issue.ignore.block.rule1.endBlockRegexp=\\/\\/end-generated
I'm using SonarQube server 5.1.2 and running the analysis from Sonar-ant-task 2.3.
I'm wondering if it may be something wrong with the regex that makes SonarQube not able to match it, or maybe some limitation in SonarQube regex processing?
EDIT: I changed the regex for something more simple to match just the '#generated' word, and it worked. But if I put '#generated[\n\r]' to force to only match if there's a new line after #generated, then it doesn't work anymore.
It seems SonarQube doesn't support basic things such as new line characters. Can someone confirm this?
I confirm that all patterns used to exclude issues are applied line per line. In the end it is always translated to "exclude issues from line X to line Y". I agree this is not perfect (especially now we have precise issue location) but this is an "historical" feature. We will probably not invest time in it, since our mood is to avoid complex configuration. The ideal solution for your use case would be to have https://jira.sonarsource.com/browse/SONARJAVA-71 implemented.
Since SonarQube applies regex per line, I came up with a different solution for this use-case:
I use the start-block regex per line: (?m)#generated$ and then exclude class|enum|interface by adding this pattern to the end-block regex, together with end-generated.
Final configuration looks like this:
sonar.issue.ignore.block=rule1
sonar.issue.ignore.block.rule1.beginBlockRegexp=(?m)#generated$
sonar.issue.ignore.block.rule1.endBlockRegexp=\/\/\h*end-generated|\sclass\s|\senum\s|\sinterface\s
Background
This relates to an older stackoverflow question. I was hoping to ask for more details but haven't got the Reputation to write comments yet.
Circumstances are the same: I'm adding codecheck warnings that I want to ignore, by editing the "IgnoredCodeIssues" section of Omnisharp's config.json file.
The question
What wildcard/regexp characters work here and how? Is it perhaps a known standard with its own docs somewhere that I can read?
Example
If I enter an issue warning verbatim it works, but it would be way more efficient to use wildcards. For example this warning:
Method 'Update' has the same with 'Start'
is a warning I don't care about and it's going to pop up a lot. A good solution would be to configure it to work for all instances of this issue, i.e. to use wildcards at the 'Update' and 'Start' parts.
Using a typical regexp it would look like this:
/(Method)\s'\w+'\shas the same with\s'\w+'/g
but that's obviously not the syntax here and would just break the config file. So I'm hoping to understand the particular syntax of wildcards in this particular file.
More details
I use Omnisharp-sublime and Sublime Text 3.
I've read the docs and rummaged around the GitHub page (no links as my reputation is too low for 2+ links) but the only relevant information is an example config file with a couple of ignored issues:
"IgnoredCodeIssues": [
"^Keyword 'private' is redundant. This is the default modifier.$",
".* should not separate words with an underscore.*"
],
EDIT:
Using
"Method '.*.' has the same with.*",
(note the .*.) makes the warnings go away but I have no idea if there are side-effects or other consequences like hiding warnings that I might do want to be notified of. Is there anyone who has seen wildcard expansions like that before? Would be great to be able to look it up and study it before adding more to my config.json
Based on the examples in the config file, you should just use standard regex inside double quotes. Don't use the Perl style of /regex/replace/options. config.json is read by the OmniSharp server, which is written in C#, so if you're looking to do anything fancy with your regexes, make sure they're C#-compatible.
So, for instance, your example regex would look like this:
"(Method)\s'\w+'\shas the same with\s'\w+'"
We use lint in our codebase at work for C/C++, I'm trying to start integrating clang-format in my workflow as well.
Unfortunately, lint occasionally requires annotations to ignore a specific check, either of the format:
/*lint -[annotation] */
or
//lint -[annotation]
Specifically, if there's a space between the opening token for the comment and 'lint', it doesn't recognize it as an annotation directive. Unfortunately, the default settings I have for clang-format see that as an error and helpfully insert the space.
Is there any way to get clang-format to recognize comments matching that pattern and leave them alone? Right now I'm using 3.4, but could upgrade if needed.
Clang-format has a `CommentPragmas' option that is
A regular expression that describes comments with special meaning, which should not be split into lines or otherwise changed.
When I put the following line in my .clang-format file, my Lint comments remain untouched.
CommentPragmas: '^lint'
Other comments that still have "lint" in them, but are not Lint comments still get formatted.
You can disable clang-format for that section of your file by using:
int formatted_code;
// clang-format off
void unformatted_code ;
// clang-format on
void formatted_code_again;
See the Disabling formating on a piece of code section.
Background
I use JScript (Microsoft's ECMAScript implementation) for a lot of Windows system administration needs. This means I use a lot of ActiveX (Automated COM) objects. The methods of these objects often expect Number or Boolean arguments. For example:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
(CreateTextFile Method on MSDN)
On the second line you see that the second argument is one that I'm talking about. A Boolean of "true" doesn't really describe how the method's behavior will change. This isn't a problem for me, but my automation-shy coworkers are easily spooked. Not knowing what an argument does spooks them. Unfortunately a long list of constants (not real constants, of course, since current JScript versions don't support them) will also spook them. So I've taken to documenting some of these basic function calls with inline block comments. The second line in the above example would be written as such:
var a = fso.CreateTextFile("c:\\testfile.txt", /*overwrite*/ true, /*unicode*/ false);
That ends up with a small syntax highlighting dilemma for me, though. I like my comments highlighted vibrantly; both block and line comments. These tiny inline block comments mean little to me, personally, however. I'd like to highlight those particular comments in a more muted fashion (light gray on white, for example). Which brings me to my dilemma.
Dilemma
I'd like to override the default syntax highlighting for block comments when both the beginning and end marks are on the same line. Ideally this is done solely in my vimrc file, and not in a superseding personal copy of the javascript.vim syntax. My initial attempt is pathetic:
hi inlineComment guifg=#bbbbbb
match inlineComment "\/\*.*\*\/"
Straight away you can see the first problem with this regular expression pattern is that it's a greedy search. It's going to match from the first "/*" to the last "*/" on the line, meaning everything between two inline block comments will get this highlight style as well. I can fix that, but I'm really not sure how to deal with my second concern.
Comments can't be defined inside of String literals in ECMAScript. So this syntax highlighting will override String highlighting as well. I've never had a problem with this in system administration scripts, but it does often bite me when I'm examining the source of many javascript libraries intended for browsers (less.js for example).
What regex pattern, syntax definition, or other solution would the amazing StackOverflow community recommend to restore my vimrc zen?
I'm not sure, but from your description it sounds like you don't need a new syntax definition. Vim syntax files usually let you override a particular syntax item with your own choice of highlighting. In this case, the item you want is called javaScriptComment, so a command like this will set its highlighting:-
hi javaScriptComment guifg=#bbbbbb
but you have to do this in your .vimrc file (or somewhere that's sourced from there), so it's evaluated before the syntax file. The syntax file uses the highlight default command, so the syntax file's choice of highlighting only affects syntax items with no highlighting set. See :help :hi-default for more details on that. BTW, it only works on Vim 5.8 and later.
The above command will change all inline /* */ comments, and leave // line comments with their default setting, because line comments are a different syntax item (javaScriptLineComment). You can find the names of all these groups by looking at the javascript.vim file. (The easiest way to do this is :e $VIMRUNTIME/syntax/javascript.vim .)
If you only want to change some inline comments, it's a little more complicated, but still easy to see what to do by looking at javascript.vim . If you do that, you can see that block comments are defined like this:-
syn region javaScriptComment start="/\*" end="\*/" contains=#Spell,javaScriptCommentTodo
See that you can use separate regexes for begin and end markers: you don't need to worry about matching the stuff in between with non-greedy quantifiers, or anything like that. To have a syntax item that works similarly but only on one line, try adding the oneline option (:h :syn-oneline for more details):-
syn region myOnelineComment start="/\*" end="\*/" oneline
I've removed the two contains groups because (1) if you're only using it for parameter names, you probably don't want spell-checking turned on inside these comments, and (2) contained sections that aren't oneline override the oneline in the container region, so you would still match all TODO comments with this region.
You can define this new kind of comment region in your .vimrc, and set the highlighting how you like: it looks like you already know how to do that, so I won't go into more details on that. I haven't tried out this particular example, so you may still need a bit of fiddling to make it work. Give it a try and let me know how it goes.
Why don't you simply add a comment line above the call?
I think that
// fso.CreateTextFile(filename:String, overwrite:Boolean, unicode:Boolean)
var a = fso.CreateTextFile("c:\\testfile.txt", true, false);
is a lot more readable and informative than
var a = fso.CreateTextFile("c:\\testfile.txt", /*overwrite*/ true, /*unicode*/ false);