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

fix file encoding support (UTF-8, UTF-16, with/without BOM) #2143

Merged
merged 1 commit into from
May 15, 2021
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ build_script:
#---------------------------------#
artifacts:
- path: 'sonar-cxx-plugin\target\*.jar'
- path: 'sslr-cxx-toolkit\target\*.jar'
- path: 'cxx-sslr-toolkit\target\*.jar'

#---------------------------------#
# global handlers #
Expand Down
5 changes: 5 additions & 0 deletions cxx-checks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<artifactId>cxx-squid</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>cxx-squid-bridge</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import org.sonar.check.Rule;
import org.sonar.cxx.tag.Tag;
import org.sonar.cxx.visitors.AbstractCxxPublicApiVisitor;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;

/**
* Check that generates issue for undocumented API items.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.AstScannerExceptionHandler;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.NoSqale;
import org.sonar.squidbridge.checks.SquidCheck;
import org.sonar.cxx.squidbridge.AstScannerExceptionHandler;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.NoSqale;
import org.sonar.cxx.squidbridge.checks.SquidCheck;

@Rule(
key = "ParsingError",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import org.sonar.check.Rule;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.NoSqale;
import org.sonar.squidbridge.checks.SquidCheck;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.NoSqale;
import org.sonar.cxx.squidbridge.checks.SquidCheck;

@Rule(
key = "ParsingErrorRecovery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import java.nio.file.Files;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.NoSqale;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import org.sonar.cxx.visitors.CxxCharsetAwareVisitor;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.NoSqale;
import org.sonar.squidbridge.checks.SquidCheck;

/**
* FileEncodingCheck
Expand All @@ -53,7 +53,7 @@ public void setCharset(Charset charset) {
@Override
public void visitFile(AstNode astNode) {
try {
Files.readAllLines(getContext().getFile().toPath(), charset);
Files.readAllLines(getContext().getInputFile().file().toPath(), charset);
} catch (IOException e) {
getContext().createFileViolation(this,
"Not all characters of the file can be encoded with the predefined charset "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@

import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.checks.SquidCheck;

@Rule(
key = "MissingNewLineAtEndOfFile",
Expand All @@ -39,24 +37,19 @@
@SqaleConstantRemediation("1min")
public class MissingNewLineAtEndOfFileCheck extends SquidCheck<Grammar> {

private static boolean endsWithNewline(RandomAccessFile randomAccessFile) throws IOException {
if (randomAccessFile.length() < 1) {
return false;
@Override
public void visitFile(AstNode astNode) {
if (isEmptyOrNotEndingWithNewLine(getContext().getInputFileContent())) {
getContext().createFileViolation(this, "Add a new line at the end of this file.");
}
randomAccessFile.seek(randomAccessFile.length() - 1);
byte lastByte = randomAccessFile.readByte();
return lastByte == '\n' || lastByte == '\r';
}

@Override
public void visitFile(AstNode astNode) {
try (var randomAccessFile = new RandomAccessFile(getContext().getFile(), "r")) {
if (!endsWithNewline(randomAccessFile)) {
getContext().createFileViolation(this, "Add a new line at the end of this file.");
}
} catch (IOException e) {
throw new IllegalStateException(e);
private static boolean isEmptyOrNotEndingWithNewLine(String content) {
if (content.isEmpty()) {
return true;
}
char lastChar = content.charAt(content.length() - 1);
return lastChar != '\n' && lastChar != '\r';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@

import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.cxx.checks.utils.CheckUtils;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import org.sonar.cxx.tag.Tag;
import org.sonar.cxx.visitors.CxxCharsetAwareVisitor;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.checks.SquidCheck;

/**
* TabCharacterCheck - similar Vera++ rule L002 "Don't use tab characters"
Expand All @@ -46,7 +40,7 @@
priority = Priority.MINOR)
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class TabCharacterCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {
public class TabCharacterCheck extends SquidCheck<Grammar> {

private static final boolean DEFAULT_CREATE_LINE_VIOLATION = false;

Expand All @@ -58,36 +52,24 @@ public class TabCharacterCheck extends SquidCheck<Grammar> implements CxxCharset
description = "Create violations per line (default is one per file)",
defaultValue = "" + DEFAULT_CREATE_LINE_VIOLATION)
public boolean createLineViolation = DEFAULT_CREATE_LINE_VIOLATION;
private Charset defaultCharset = StandardCharsets.UTF_8;

@Override
public void setCharset(Charset charset) {
this.defaultCharset = charset;
}

@Override
public void visitFile(AstNode astNode) {
try ( var br = new BufferedReader(CheckUtils.getInputSteam(getContext().getFile(), defaultCharset))) {
String line;
int nr = 0;

while ((line = br.readLine()) != null) {
++nr;
if (line.contains("\t")) {
if (createLineViolation) {
getContext().createLineViolation(
this,
"Replace all tab characters in this line by sequences of white-spaces.", nr);
} else {
getContext().createFileViolation(
this,
"Replace all tab characters in this file by sequences of white-spaces.");
break;
}
int nr = 0;
for (String line : getContext().getInputFileLines()) {
++nr;
if (line.contains("\t")) {
if (createLineViolation) {
getContext().createLineViolation(
this,
"Replace all tab characters in this line by sequences of white-spaces.", nr);
} else {
getContext().createFileViolation(
this,
"Replace all tab characters in this file by sequences of white-spaces.");
break;
}
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.sonar.check.RuleProperty;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.SqaleLinearWithOffsetRemediation;
import org.sonar.cxx.squidbridge.annotations.SqaleLinearWithOffsetRemediation;

@Rule(
key = "ClassComplexity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.sonar.cxx.visitors.MultiLocatitionSquidCheck;

/**
* This is an enhanced version of org.sonar.squidbridge.metrics.ComplexityVisitor, which is used in order to compute the
* This is an enhanced version of org.sonar.cxx.squidbridge.metrics.ComplexityVisitor, which is used in order to compute the
* Cyclomatic Complexity.
*
* @param <G>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.SqaleLinearWithOffsetRemediation;
import org.sonar.cxx.squidbridge.annotations.SqaleLinearWithOffsetRemediation;

@Rule(
key = "FileComplexity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.sonar.cxx.utils.CxxReportIssue;
import org.sonar.cxx.visitors.CxxCognitiveComplexityVisitor;
import org.sonar.cxx.visitors.CxxComplexityScope;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleLinearWithOffsetRemediation;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleLinearWithOffsetRemediation;

@Rule(
key = "FunctionCognitiveComplexity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.sonar.check.RuleProperty;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleLinearWithOffsetRemediation;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleLinearWithOffsetRemediation;

@Rule(
key = "FunctionComplexity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@

import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.cxx.checks.utils.CheckUtils;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import org.sonar.cxx.tag.Tag;
import org.sonar.cxx.visitors.CxxCharsetAwareVisitor;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.checks.SquidCheck;

/**
* TooLongLineCheck - similar Vera++ rule L004 "Line too long"
Expand All @@ -46,7 +40,7 @@
priority = Priority.MINOR)
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class TooLongLineCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {
public class TooLongLineCheck extends SquidCheck<Grammar> {

private static final int DEFAULT_MAXIMUM_LINE_LENHGTH = 160;
private static final int DEFAULT_TAB_WIDTH = 8;
Expand All @@ -69,32 +63,19 @@ public class TooLongLineCheck extends SquidCheck<Grammar> implements CxxCharsetA
defaultValue = "" + DEFAULT_TAB_WIDTH)
public int tabWidth = DEFAULT_TAB_WIDTH;

private Charset defaultCharset = StandardCharsets.UTF_8;

@Override
public void setCharset(Charset charset) {
this.defaultCharset = charset;
}

@Override
public void visitFile(AstNode astNode) {
try ( var br = new BufferedReader(CheckUtils.getInputSteam(getContext().getFile(), defaultCharset))) {
String line;
int nr = 0;

while ((line = br.readLine()) != null) {
++nr;
long length = line.chars().filter(c -> c == '\t').count();
length = line.length() + length * (tabWidth - 1);
if (length > maximumLineLength) {
getContext().createLineViolation(
this,
"Split this {0} characters long line (which is greater than {1} authorized).",
nr, length, maximumLineLength);
}
int nr = 0;
for (String line : getContext().getInputFileLines()) {
++nr;
long length = line.chars().filter(c -> c == '\t').count();
length = line.length() + length * (tabWidth - 1);
if (length > maximumLineLength) {
getContext().createLineViolation(
this,
"Split this {0} characters long line (which is greater than {1} authorized).",
nr, length, maximumLineLength);
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.sonar.check.RuleProperty;
import org.sonar.cxx.api.CxxMetric;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.checks.ChecksHelper;
import org.sonar.squidbridge.checks.SquidCheck;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.checks.ChecksHelper;
import org.sonar.cxx.squidbridge.checks.SquidCheck;

@Rule(
key = "TooManyLinesOfCodeInFile",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import org.sonar.cxx.api.CxxMetric;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.api.SourceFunction;
import org.sonar.squidbridge.checks.SquidCheck;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.api.SourceFunction;
import org.sonar.cxx.squidbridge.checks.SquidCheck;

@Rule(key = "TooManyLinesOfCodeInFunction",
name = "Avoid too many code lines in a single function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import org.sonar.check.RuleProperty;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.checks.SquidCheck;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.checks.SquidCheck;

@Rule(
key = "TooManyParameters",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.parser.CxxKeyword;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.checks.AbstractOneStatementPerLineCheck;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.checks.AbstractOneStatementPerLineCheck;

/**
* TooManyStatementsPerLineCheck - Statements should be on separate lines
Expand Down
Loading