Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve FileLinesVisitor #1014

Merged
merged 1 commit into from
Dec 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions integration-tests/features/importing_coverage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ Feature: Importing coverage data
"""
AND the following metrics have following values:
| metric | value |
| coverage | 5.9 |
| line_coverage | 3.1 |
| coverage | 8.0 |
| line_coverage | 4.3 |
| branch_coverage | 50 |
| it_coverage | 14.1 |
| it_line_coverage | 8.2 |
| it_coverage | 18.2 |
| it_line_coverage | 11.1 |
| it_branch_coverage | 50 |
| overall_coverage | 20.0 |
| overall_line_coverage | 12.5 |
| overall_coverage | 25.0 |
| overall_line_coverage | 16.7 |
| overall_branch_coverage | 50 |


Expand All @@ -115,8 +115,8 @@ Feature: Importing coverage data
"""
AND the following metrics have following values:
| metric | value |
| coverage | 16.9 |
| line_coverage | 10.1 |
| coverage | 22.2 |
| line_coverage | 14.3 |
| branch_coverage | 50 |


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.cxx.api.CxxMetric;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.squidbridge.SquidAstVisitor;

/**
Expand All @@ -49,20 +50,33 @@ public class FileLinesVisitor extends SquidAstVisitor<Grammar> implements AstAnd
private Set<Integer> linesOfComments = Sets.newHashSet();
private final FileSystem fileSystem;
private final Map<InputFile, Set<Integer>> allLinesOfCode;
private int isWithinFunctionDefinition = 0;
private final Set<String> ignoreToken = Sets.newHashSet(
"{", "}", "(", ")", "[", "]"
);

public FileLinesVisitor(FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem, Map<InputFile, Set<Integer>> linesOfCode) {
this.fileLinesContextFactory = fileLinesContextFactory;
this.fileSystem = fileSystem;
this.allLinesOfCode = linesOfCode;
}

@Override
public void init() {
subscribeTo(CxxGrammarImpl.functionDefinition);
}

@Override
public void visitToken(Token token) {
if (token.getType().equals(GenericTokenType.EOF)) {
return;
}

linesOfCode.add(token.getLine());
if (isWithinFunctionDefinition != 0) {
if (!ignoreToken.contains(token.getType().getValue())) {
linesOfCode.add(token.getLine());
}
}

List<Trivia> trivias = token.getTrivia();
for (Trivia trivia : trivias) {
Expand All @@ -72,6 +86,16 @@ public void visitToken(Token token) {
}
}

@Override
public void visitNode(AstNode node) {
isWithinFunctionDefinition++;
}

@Override
public void leaveNode(AstNode node) {
isWithinFunctionDefinition--;
}

@Override
public void leaveFile(AstNode astNode) {
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().is(getContext().getFile()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ public void test() throws UnsupportedEncodingException, IOException {

assertThat(linesOfCode).hasSize(1);
assertThat(linesOfCode.get(inputFile)).containsOnly(
8, 9, 10, 11,
14, 15, 16, 17, 18, 19,
21, 22, 23, 24, 25, 26, 27,
31, 32, 34, 35, 36, 37,
42, 43, 44, 45, 46
8, 10,
14, 16, 17, 18,
21, 22, 23, 26,
31, 34, 35,
42, 44, 45,
57, 60, 61
);
}

Expand Down
18 changes: 18 additions & 0 deletions sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/ncloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,22 @@ void func5()
int sum = ADD(a, b);
}

// declaration
void func6();

// inline
class MyClass {
public:
MyClass();
int method1();

int method2()
{
// comment
for(int iii=0; iii<NUMBER; ++iii) {
h1 += iii; // comment
}
}
};

/* EOF */