-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from guwirth/enhancement/line_regex
Line RegEx rule
- Loading branch information
Showing
7 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
cxx-checks/src/main/java/org/sonar/cxx/checks/LineRegularExpressionCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2011 Waleri Enns and CONTACT Software GmbH | ||
* dev@sonar.codehaus.org | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.cxx.checks; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.regex.PatternSyntaxException; | ||
import com.google.common.io.Files; | ||
|
||
import org.sonar.check.Cardinality; | ||
import org.sonar.check.Priority; | ||
import org.sonar.check.Rule; | ||
import org.sonar.check.RuleProperty; | ||
import com.sonar.sslr.api.Grammar; | ||
import org.sonar.api.utils.SonarException; | ||
import org.sonar.cxx.visitors.CxxCharsetAwareVisitor; | ||
import org.sonar.squidbridge.checks.SquidCheck; | ||
import org.sonar.api.utils.PathUtils; | ||
import org.sonar.api.utils.WildcardPattern; | ||
import com.sonar.sslr.api.AstNode; | ||
|
||
@Rule( | ||
key = "LineRegularExpression", | ||
cardinality = Cardinality.MULTIPLE, | ||
priority = Priority.MAJOR) | ||
|
||
public class LineRegularExpressionCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor { | ||
|
||
private static final String DEFAULT_MATCH_FILE_PATTERN = ""; | ||
private static final String DEFAULT_REGULAR_EXPRESSION = ""; | ||
private static final String DEFAULT_MESSAGE = "The regular expression matches this line"; | ||
|
||
private Charset charset; | ||
private Pattern pattern; | ||
|
||
@RuleProperty( | ||
key = "matchFilePattern", | ||
defaultValue = DEFAULT_MATCH_FILE_PATTERN) | ||
public String matchFilePattern = DEFAULT_MATCH_FILE_PATTERN; | ||
|
||
@RuleProperty( | ||
key = "regularExpression", | ||
defaultValue = DEFAULT_REGULAR_EXPRESSION) | ||
public String regularExpression = DEFAULT_REGULAR_EXPRESSION; | ||
|
||
@RuleProperty( | ||
key = "message", | ||
defaultValue = DEFAULT_MESSAGE) | ||
public String message = DEFAULT_MESSAGE; | ||
|
||
@Override | ||
public void init() { | ||
try { | ||
pattern = Pattern.compile(regularExpression); | ||
} catch (PatternSyntaxException e) { | ||
throw new SonarException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void setCharset(Charset charset) { | ||
this.charset = charset; | ||
} | ||
|
||
@Override | ||
public void visitFile(AstNode fileNode) { | ||
if (fileNode != null) { | ||
List<String> lines; | ||
try { | ||
lines = Files.readLines(getContext().getFile(), charset); | ||
} catch (IOException e) { | ||
throw new SonarException(e); | ||
} | ||
for (int i = 0; i < lines.size(); ++i) { | ||
Matcher matcher = pattern.matcher(lines.get(i)); | ||
if (matcher.find()) { | ||
getContext().createLineViolation(this, message, i + 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private boolean matchFile() { | ||
if (!matchFilePattern.isEmpty()) { | ||
WildcardPattern filePattern = WildcardPattern.create(matchFilePattern); | ||
String path = PathUtils.sanitize(getContext().getFile().getPath()); | ||
return filePattern.match(path); | ||
} | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
cxx-checks/src/main/resources/org/sonar/l10n/cxx/rules/cxx/LineRegularExpression.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<p> | ||
This rule template can be used to create rules which will be triggered when a line matches a given regular expression. | ||
For each match a line violation will be created. | ||
Additional a Ant-style matching pattern for the path can be defined. | ||
</p> | ||
|
||
<p> | ||
matchFilePattern allows Ant-style matching patterns for the path. If no matchFilePattern is defined all files are evaluated. | ||
</p> | ||
|
||
<p> | ||
Following rules are applied: | ||
</p> | ||
<ul> | ||
<li>? matches single character</li> | ||
<li>* matches zero or more characters</li> | ||
<li>** matches zero or more 'directories'</li> | ||
<li>use always '/' as a directory separator</li> | ||
<li>there must always be a root directory</li> | ||
</ul> | ||
|
||
<p> | ||
Some examples of patterns: | ||
</p> | ||
<ul> | ||
<li>/**/*.cpp - matches all .cpp files in all directories (you have to define the root directory '/'), e.g. org/Foo.cpp or org/foo/Bar.cpp or org/foo/bar/Baz.cpp</li> | ||
<li>/**/test/**/Foo.cpp - matches all 'Foo.cpp' files in directories with one 'test' directory in the path, e.g. org/test/Foo.cpp or org/bar/test/bar/Foo.cpp</li> | ||
<li>org/T?st.cpp - matches org/Test.cpp and also org/Tost.cpp</li> | ||
<li>org/*.cpp - matches all .cpp files in the org directory, e.g. org/Foo.cpp or org/Bar.cpp</li> | ||
<li>org/** - matches all files underneath the org directory, e.g. org/Foo.cpp or org/foo/bar.jsp</li> | ||
<li>org/**/Test.cpp - matches all Test.cpp files underneath the org directory, e.g. org/Test.cpp or org/foo/Test.cpp or org/foo/bar/Test.cpp</li> | ||
<li>org/**/*.cpp - matches all .cpp files underneath the org directory, e.g. org/Foo.cpp or org/foo/Bar.cpp or org/foo/bar/Baz.cpp</li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
cxx-checks/src/test/java/org/sonar/cxx/checks/LineRegularExpressionCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2011 Waleri Enns and CONTACT Software GmbH | ||
* dev@sonar.codehaus.org | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.cxx.checks; | ||
|
||
import java.io.File; | ||
|
||
import org.junit.Test; | ||
import org.sonar.cxx.CxxAstScanner; | ||
import org.sonar.squidbridge.api.SourceFile; | ||
import org.sonar.squidbridge.checks.CheckMessagesVerifier; | ||
|
||
public class LineRegularExpressionCheckTest { | ||
|
||
@Test | ||
public void lineRegExWithoutFilePattern() { | ||
LineRegularExpressionCheck check = new LineRegularExpressionCheck(); | ||
check.regularExpression = "stdafx\\.h"; | ||
check.message = "Found 'stdafx.h' in line!"; | ||
|
||
SourceFile file = CxxAstScanner.scanSingleFile(new File("src/test/resources/checks/LineRegEx.cc"), check); | ||
CheckMessagesVerifier.verify(file.getCheckMessages()) | ||
.next().atLine(2).withMessage(check.message) | ||
.next().atLine(3).withMessage(check.message) | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void lineRegExWithFilePattern() { | ||
LineRegularExpressionCheck check = new LineRegularExpressionCheck(); | ||
check.matchFilePattern = "/**/*.cc"; // all files with .cc file extension | ||
check.regularExpression = "#include\\s+\"stdafx\\.h\""; | ||
check.message = "Found '#include \"stdafx.h\"' in line in a .cc file!"; | ||
|
||
SourceFile file = CxxAstScanner.scanSingleFile(new File("src/test/resources/checks/LineRegEx.cc"), check); | ||
CheckMessagesVerifier.verify(file.getCheckMessages()) | ||
.next().atLine(2).withMessage(check.message) | ||
.next().atLine(3).withMessage(check.message) | ||
.noMore(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Comment | ||
#include "stdafx.h" | ||
#include "stdafx.h" | ||
int i; | ||
void func() {} | ||
|