aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorShevek <[email protected]>2015-01-01 11:06:48 -0800
committerShevek <[email protected]>2015-01-01 11:06:48 -0800
commit34f2c3f91099b19efc4ce4c5d26b8b8eedfb3aa8 (patch)
tree5d415c0d04cf57d9e2208e92a0f9725831f601cc /src
parent3e0f34f77c1ea1f9b27b097efaa8bcf3dee19e4b (diff)
Fix #21: Allow including absolute files.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/anarres/cpp/Preprocessor.java10
-rw-r--r--src/test/java/org/anarres/cpp/IncludeAbsoluteTest.java34
2 files changed, 44 insertions, 0 deletions
diff --git a/src/main/java/org/anarres/cpp/Preprocessor.java b/src/main/java/org/anarres/cpp/Preprocessor.java
index 8b1ddcc..5dacc91 100644
--- a/src/main/java/org/anarres/cpp/Preprocessor.java
+++ b/src/main/java/org/anarres/cpp/Preprocessor.java
@@ -1150,6 +1150,16 @@ public class Preprocessor implements Closeable {
@Nonnull String name, boolean quoted, boolean next)
throws IOException,
LexerException {
+ if (name.startsWith("/")) {
+ VirtualFile file = filesystem.getFile(name);
+ if (include(file))
+ return;
+ StringBuilder buf = new StringBuilder();
+ buf.append("File not found: ").append(name);
+ error(line, 0, buf.toString());
+ return;
+ }
+
VirtualFile pdir = null;
if (quoted) {
if (parent != null) {
diff --git a/src/test/java/org/anarres/cpp/IncludeAbsoluteTest.java b/src/test/java/org/anarres/cpp/IncludeAbsoluteTest.java
new file mode 100644
index 0000000..1a20a70
--- /dev/null
+++ b/src/test/java/org/anarres/cpp/IncludeAbsoluteTest.java
@@ -0,0 +1,34 @@
+package org.anarres.cpp;
+
+import java.io.BufferedReader;
+import java.io.Reader;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * @author shevek
+ */
+public class IncludeAbsoluteTest {
+
+ private static final Logger LOG = LoggerFactory.getLogger(IncludeAbsoluteTest.class);
+
+ // TODO: Rewrite this test to get the CWD and read a file with known content in the test suite.
+ // Guava (available in test suite) can map a URL to a File resource.
+ @Test
+ public void testAbsoluteInclude() throws Exception {
+ Preprocessor pp = new Preprocessor();
+ pp.getSystemIncludePath().add("/usr/include");
+ pp.addInput(new StringLexerSource(
+ "#include </usr/include/features.h>\n"
+ + "", true));
+ Reader r = new CppReader(pp);
+ // This will error if the file isn't found.
+ BufferedReader br = new BufferedReader(r);
+ for (int i = 0; i < 10; i++) {
+ LOG.info(br.readLine());
+ }
+ br.close();
+ }
+}