-
Notifications
You must be signed in to change notification settings - Fork 363
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
workaround for 'forceZeroCoverage' #1007
Merged
guwirth
merged 1 commit into
SonarOpenCommunity:master
from
guwirth:forceZeroCoverage-workaround
Dec 21, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
100 changes: 100 additions & 0 deletions
100
sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/metrics/FileLinesVisitor.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,100 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2010-2016 SonarOpenCommunity | ||
* http://github.com/SonarOpenCommunity/sonar-cxx | ||
* | ||
* 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 02110-1301, USA. | ||
*/ | ||
package org.sonar.plugins.cxx.metrics; | ||
|
||
import com.google.common.collect.Sets; | ||
import com.sonar.sslr.api.AstAndTokenVisitor; | ||
import com.sonar.sslr.api.AstNode; | ||
import com.sonar.sslr.api.GenericTokenType; | ||
import com.sonar.sslr.api.Grammar; | ||
import com.sonar.sslr.api.Token; | ||
import com.sonar.sslr.api.Trivia; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.sonar.api.batch.fs.FileSystem; | ||
import org.sonar.api.batch.fs.InputFile; | ||
import org.sonar.api.measures.CoreMetrics; | ||
import org.sonar.api.measures.FileLinesContext; | ||
import org.sonar.api.measures.FileLinesContextFactory; | ||
import org.sonar.cxx.api.CxxMetric; | ||
import org.sonar.squidbridge.SquidAstVisitor; | ||
|
||
/** | ||
* Visitor that computes {@link CoreMetrics#NCLOC_DATA_KEY} and | ||
* {@link CoreMetrics#COMMENT_LINES_DATA_KEY} metrics used by the DevCockpit. | ||
*/ | ||
public class FileLinesVisitor extends SquidAstVisitor<Grammar> implements AstAndTokenVisitor { | ||
|
||
private final FileLinesContextFactory fileLinesContextFactory; | ||
|
||
private Set<Integer> linesOfCode = Sets.newHashSet(); | ||
private Set<Integer> linesOfComments = Sets.newHashSet(); | ||
private final FileSystem fileSystem; | ||
private final Map<InputFile, Set<Integer>> allLinesOfCode; | ||
|
||
public FileLinesVisitor(FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem, Map<InputFile, Set<Integer>> linesOfCode) { | ||
this.fileLinesContextFactory = fileLinesContextFactory; | ||
this.fileSystem = fileSystem; | ||
this.allLinesOfCode = linesOfCode; | ||
} | ||
|
||
@Override | ||
public void visitToken(Token token) { | ||
if (token.getType().equals(GenericTokenType.EOF)) { | ||
return; | ||
} | ||
|
||
linesOfCode.add(token.getLine()); | ||
|
||
List<Trivia> trivias = token.getTrivia(); | ||
for (Trivia trivia : trivias) { | ||
if (trivia.isComment()) { | ||
linesOfComments.add(trivia.getToken().getLine()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void leaveFile(AstNode astNode) { | ||
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().is(getContext().getFile())); | ||
if (inputFile == null) { | ||
throw new IllegalStateException("InputFile is null, but it should not be."); | ||
} | ||
FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(inputFile); | ||
|
||
int fileLength = getContext().peekSourceCode().getInt(CxxMetric.LINES); | ||
for (int line = 1; line <= fileLength; line++) { | ||
if (linesOfCode.contains(line)) { | ||
fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, 1); | ||
} | ||
if (linesOfComments.contains(line)) { | ||
fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, 1); | ||
} | ||
} | ||
fileLinesContext.save(); | ||
|
||
allLinesOfCode.put(inputFile, linesOfCode); | ||
|
||
linesOfCode = Sets.newHashSet(); | ||
linesOfComments = Sets.newHashSet(); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be called filesexecutedlinesvisitor?