Skip to content

Commit de6d343

Browse files
authored
Merge pull request #2142 from guwirth/fix-2139
support BOM in #include files
2 parents 066232c + 54ef3e1 commit de6d343

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

cxx-squid/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
<artifactId>jackson-databind</artifactId>
4444
<version>2.12.3</version>
4545
</dependency>
46+
<dependency>
47+
<groupId>commons-io</groupId>
48+
<artifactId>commons-io</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>commons-lang</groupId>
52+
<artifactId>commons-lang</artifactId>
53+
</dependency>
4654
<dependency>
4755
<groupId>org.sonarsource.sslr</groupId>
4856
<artifactId>sslr-testing-harness</artifactId>

cxx-squid/src/main/java/org/sonar/cxx/preprocessor/SourceCodeProvider.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.sonar.cxx.preprocessor;
2121

2222
import java.io.File;
23+
import java.io.FileInputStream;
2324
import java.io.IOException;
2425
import java.nio.charset.Charset;
2526
import java.nio.file.Files;
@@ -32,6 +33,8 @@
3233
import java.util.Objects;
3334
import javax.annotation.CheckForNull;
3435
import javax.annotation.Nullable;
36+
import org.apache.commons.io.ByteOrderMark;
37+
import org.apache.commons.io.input.BOMInputStream;
3538
import org.sonar.api.utils.log.Logger;
3639
import org.sonar.api.utils.log.Loggers;
3740

@@ -199,9 +202,18 @@ public File getSourceCodeFile(String filename, boolean quoted) {
199202
return result;
200203
}
201204

202-
public String getSourceCode(File file, Charset charset) throws IOException {
203-
byte[] encoded = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
204-
return new String(encoded, charset);
205+
public String getSourceCode(File file, Charset defaultCharset) throws IOException {
206+
try (BOMInputStream bomInputStream = new BOMInputStream(new FileInputStream(file),
207+
ByteOrderMark.UTF_8,
208+
ByteOrderMark.UTF_16LE,
209+
ByteOrderMark.UTF_16BE,
210+
ByteOrderMark.UTF_32LE,
211+
ByteOrderMark.UTF_32BE)) {
212+
ByteOrderMark bom = bomInputStream.getBOM();
213+
Charset charset = bom != null ? Charset.forName(bom.getCharsetName()) : defaultCharset;
214+
byte[] bytes = bomInputStream.readAllBytes();
215+
return new String(bytes, charset);
216+
}
205217
}
206218

207219
private static class State {

cxx-squid/src/test/java/org/sonar/cxx/preprocessor/SourceCodeProviderTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class SourceCodeProviderTest {
3232

3333
private final File expected1 = new File(new File("src/test/resources/codeprovider/source.hh").getAbsolutePath());
3434
private final File expected2 = new File(new File("src/test/resources/codeprovider/source").getAbsolutePath());
35+
private final File root = new File(new File("src/test/resources/codeprovider").getAbsolutePath());
3536

3637
// ////////////////////////////////////////////////////////////////////////////
3738
// Behavior in the absolute path case
@@ -205,4 +206,26 @@ public void getting_source_code2() throws IOException {
205206
assertEquals("source code", codeProvider.getSourceCode(expected2, Charset.defaultCharset()));
206207
}
207208

209+
@Test
210+
public void getting_source_code_utf_8() throws IOException {
211+
SourceCodeProvider codeProvider = new SourceCodeProvider(new File("dummy"));
212+
assertEquals("UTF-8", codeProvider.getSourceCode(new File(root, "./utf-8.hh"),
213+
Charset.defaultCharset()));
214+
}
215+
216+
@Test
217+
public void getting_source_code_utf_8_bom() throws IOException {
218+
SourceCodeProvider codeProvider = new SourceCodeProvider(new File("dummy"));
219+
assertEquals("UTF-8-BOM", codeProvider.getSourceCode(new File(root, "./utf-8-bom.hh"),
220+
Charset.defaultCharset()));
221+
}
222+
223+
@Test
224+
public void getting_source_code_utf_16_le_bom() throws IOException {
225+
SourceCodeProvider codeProvider = new SourceCodeProvider(new File("dummy"));
226+
assertEquals("UTF-16LE-BOM",
227+
codeProvider.getSourceCode(new File(root, "./utf-16le-bom.hh"),
228+
Charset.defaultCharset()));
229+
}
230+
208231
}
26 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UTF-8-BOM
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UTF-8

0 commit comments

Comments
 (0)