From e38a59d3a0c51ee5548ea070945cb18f9aef45a7 Mon Sep 17 00:00:00 2001 From: Shevek Date: Thu, 8 May 2008 19:33:22 +0000 Subject: fix more to spec --- src/input/test.c | 61 ------------------ src/input/test0.c | 61 ++++++++++++++++++ src/input/test0.h | 2 +- src/input/test1.c | 2 + src/java/org/anarres/cpp/Main.java | 61 +++++++----------- src/java/org/anarres/cpp/Preprocessor.java | 79 +++++++++++++++++------- src/tests/org/anarres/cpp/CppReaderTestCase.java | 2 +- src/tests/org/anarres/cpp/MainTestCase.java | 2 +- 8 files changed, 147 insertions(+), 123 deletions(-) delete mode 100644 src/input/test.c create mode 100644 src/input/test0.c create mode 100644 src/input/test1.c diff --git a/src/input/test.c b/src/input/test.c deleted file mode 100644 index 150b759..0000000 --- a/src/input/test.c +++ /dev/null @@ -1,61 +0,0 @@ -line = __LINE__ -file = __FILE__ - -#define A a /* a defined */ -#define B b /* b defined */ -#define C c /* c defined */ - -#define EXPAND(x) x -EXPAND(a) -> a -EXPAND(A) -> a - -#define _STRINGIFY(x) #x -_STRINGIFY(A) -> "A" - -#define STRINGIFY(x) _STRINGIFY(x) -STRINGIFY(b) -> "b" -STRINGIFY(A) -> "a" - -#define _CONCAT(x, y) x ## y -_CONCAT(A, B) -> AB - -#define A_CONCAT done_a_concat -_CONCAT(A, _CONCAT(B, C)) -> done_a_concat(b, c) - -#define CONCAT(x, y) _CONCAT(x, y) -CONCAT(A, CONCAT(B, C)) -> abc - -#define _CONCAT3(x, y, z) x ## y ## z -_CONCAT3(a, b, c) -> abc -_CONCAT3(A, B, C) -> ABC -_CONCAT3(A, EXPAND(B), C) -> AEXPAND(b)C - -Line is __LINE__ -File is __FILE__ - -#define two three -one /* one */ -#define one two -one /* three */ -#undef two -#define two five -one /* five */ -#undef two -one /* two */ -#undef one -#define one four -one /* four */ -#undef one -#define one one -one /* one */ - -/* warning line 57 column 0 */ -#warning arse - -#define foo(x) foo(x, b) -foo(1) -> _foo(1, b) without the _ -foo(foo(2)) -> _foo(_foo(2, b), b) without the _ -foo(y, z) - -#define var(x...) a x b -var(e, f, g) -> a e, f, g b diff --git a/src/input/test0.c b/src/input/test0.c new file mode 100644 index 0000000..150b759 --- /dev/null +++ b/src/input/test0.c @@ -0,0 +1,61 @@ +line = __LINE__ +file = __FILE__ + +#define A a /* a defined */ +#define B b /* b defined */ +#define C c /* c defined */ + +#define EXPAND(x) x +EXPAND(a) -> a +EXPAND(A) -> a + +#define _STRINGIFY(x) #x +_STRINGIFY(A) -> "A" + +#define STRINGIFY(x) _STRINGIFY(x) +STRINGIFY(b) -> "b" +STRINGIFY(A) -> "a" + +#define _CONCAT(x, y) x ## y +_CONCAT(A, B) -> AB + +#define A_CONCAT done_a_concat +_CONCAT(A, _CONCAT(B, C)) -> done_a_concat(b, c) + +#define CONCAT(x, y) _CONCAT(x, y) +CONCAT(A, CONCAT(B, C)) -> abc + +#define _CONCAT3(x, y, z) x ## y ## z +_CONCAT3(a, b, c) -> abc +_CONCAT3(A, B, C) -> ABC +_CONCAT3(A, EXPAND(B), C) -> AEXPAND(b)C + +Line is __LINE__ +File is __FILE__ + +#define two three +one /* one */ +#define one two +one /* three */ +#undef two +#define two five +one /* five */ +#undef two +one /* two */ +#undef one +#define one four +one /* four */ +#undef one +#define one one +one /* one */ + +/* warning line 57 column 0 */ +#warning arse + +#define foo(x) foo(x, b) +foo(1) -> _foo(1, b) without the _ +foo(foo(2)) -> _foo(_foo(2, b), b) without the _ +foo(y, z) + +#define var(x...) a x b +var(e, f, g) -> a e, f, g b diff --git a/src/input/test0.h b/src/input/test0.h index 72db7b7..b6697c6 100644 --- a/src/input/test0.h +++ b/src/input/test0.h @@ -1,7 +1,7 @@ test0start_2 -#include +#include "test1.h" test0end___6 diff --git a/src/input/test1.c b/src/input/test1.c new file mode 100644 index 0000000..3e6fbda --- /dev/null +++ b/src/input/test1.c @@ -0,0 +1,2 @@ +#include "./test0.h" +#include diff --git a/src/java/org/anarres/cpp/Main.java b/src/java/org/anarres/cpp/Main.java index 253b9ad..e62ca83 100644 --- a/src/java/org/anarres/cpp/Main.java +++ b/src/java/org/anarres/cpp/Main.java @@ -53,15 +53,6 @@ public class Main { } } - private List i_default; - private List i_system; - private List i_user; - private List i_quote; - private Map d_default; - private Map d_user; - private Set warnings; - private List f_include; - private static final Option[] OPTS = new Option[] { new Option("help", LongOpt.NO_ARGUMENT, 'h', null, "Displays help and usage information."), @@ -69,15 +60,17 @@ public class Main { "Defines the given macro."), new Option("undefine", LongOpt.REQUIRED_ARGUMENT, 'U', "name", "Undefines the given macro, previously either builtin or defined using -D."), - new Option("include", LongOpt.REQUIRED_ARGUMENT, 'i', "file", + new Option("include", LongOpt.REQUIRED_ARGUMENT, 1, "file", "Process file as if \"#" + "include \"file\"\" appeared as the first line of the primary source file."), new Option("incdir", LongOpt.REQUIRED_ARGUMENT, 'I', "dir", "Adds the directory dir to the list of directories to be searched for header files."), + new Option("iquote", LongOpt.REQUIRED_ARGUMENT, 0, "dir", + "Adds the directory dir to the list of directories to be searched for header files included using \"\"."), new Option("warning", LongOpt.REQUIRED_ARGUMENT, 'W', "type", "Enables the named warning class (" + getWarnings() + ")."), new Option("no-warnings", LongOpt.NO_ARGUMENT, 'w', null, "Disables ALL warnings."), - new Option("version", LongOpt.NO_ARGUMENT, 'V', null, + new Option("version", LongOpt.NO_ARGUMENT, 2, null, "Prints jcpp's version number (" + Version.getVersion() + ")"), }; @@ -96,15 +89,6 @@ public class Main { (new Main()).run(OPTS, args); } - public Main() { - i_default = new ArrayList(); - i_system = new ArrayList(); - i_user = new ArrayList(); - i_quote = new ArrayList(); - d_default = new HashMap(); - d_user = new HashMap(); - } - public void run(Option[] opts, String[] args) throws Exception { String sopts = getShortOpts(opts); Getopt g = new Getopt("jcpp", args, sopts, opts); @@ -115,23 +99,27 @@ public class Main { Preprocessor pp = new Preprocessor(); pp.addFeature(Feature.LINEMARKERS); + pp.addMacro("__JCPP__"); + GETOPT: while ((c = g.getopt()) != -1) { switch (c) { case 'D': arg = g.getOptarg(); idx = arg.indexOf('='); if (idx == -1) - d_user.put(arg, "1"); + pp.addMacro(arg); else - d_user.put(arg.substring(0, idx), + pp.addMacro(arg.substring(0, idx), arg.substring(idx + 1)); break; case 'U': - d_default.remove(g.getOptarg()); - d_user.remove(g.getOptarg()); + pp.getMacros().remove(g.getOptarg()); break; case 'I': - i_user.add(g.getOptarg()); + pp.getSystemIncludePath().add(g.getOptarg()); + break; + case 0: // --iquote= + pp.getQuoteIncludePath().add(g.getOptarg()); break; case 'W': arg = g.getOptarg().toUpperCase(); @@ -144,14 +132,14 @@ public class Main { case 'w': pp.getWarnings().clear(); break; - case 'i': + case 1: // --include= // pp.addInput(new File(g.getOptarg())); // Comply exactly with spec. pp.addInput(new StringLexerSource( "#" + "include \"" + g.getOptarg() + "\"\n" )); break; - case 'V': + case 2: // --version System.out.println("Anarres Java C Preprocessor version " + Version.getVersion()); System.out.println("Copyright (C) 2008 Shevek (http://www.anarres.org/)."); System.out.println("This is free software; see the source for copying conditions. There is NO"); @@ -166,14 +154,10 @@ public class Main { } } - /* XXX include-path, include-files. */ - - for (Map.Entry e : d_default.entrySet()) - pp.addMacro(e.getKey(), e.getValue()); - for (Map.Entry e : d_user.entrySet()) - pp.addMacro(e.getKey(), e.getValue()); - - /* XXX Include paths. */ + List path = pp.getSystemIncludePath(); + path.add("/usr/local/include"); + path.add("/usr/include"); + path.add("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include"); for (int i = g.getOptind(); i < args.length; i++) pp.addInput(new FileLexerSource(new File(args[i]))); @@ -203,6 +187,8 @@ public class Main { StringBuilder buf = new StringBuilder(); for (int i = 0; i < opts.length; i++) { char c = (char)opts[i].getVal(); + if (!Character.isLetterOrDigit(c)) + continue; for (int j = 0; j < buf.length(); j++) if (buf.charAt(j) == c) throw new Exception( @@ -294,7 +280,8 @@ public class Main { line.append('=').append(opt.eg); break; } - line.append(" (-").append((char)opt.getVal()).append(")"); + if (Character.isLetterOrDigit(opt.getVal())) + line.append(" (-").append((char)opt.getVal()).append(")"); if (line.length() < 30) { while (line.length() < 30) line.append(' '); @@ -323,7 +310,7 @@ public class Main { Source source = new FileLexerSource(new File(args[0])); Preprocessor pp = new Preprocessor(source); - pp.setIncludePath(path); + pp.setSystemIncludePath(path); for (int i = 1; i < args.length; i++) { pp.push_source(new FileLexerSource(new File(args[i])),true); diff --git a/src/java/org/anarres/cpp/Preprocessor.java b/src/java/org/anarres/cpp/Preprocessor.java index 63304b0..b3b1169 100644 --- a/src/java/org/anarres/cpp/Preprocessor.java +++ b/src/java/org/anarres/cpp/Preprocessor.java @@ -54,7 +54,8 @@ public class Preprocessor { private Stack states; private Source source; - private List path; + private List quoteincludepath; /* -iquote */ + private List sysincludepath; /* -I */ private Set features; private Set warnings; private PreprocessorListener listener; @@ -66,7 +67,8 @@ public class Preprocessor { this.states = new Stack(); states.push(new State()); this.source = null; - this.path = null; + this.quoteincludepath = new ArrayList(); + this.sysincludepath = new ArrayList(); this.features = EnumSet.noneOf(Feature.class); features.add(Feature.DIGRAPHS); features.add(Feature.TRIGRAPHS); @@ -230,11 +232,27 @@ public class Preprocessor { } /** - * Sets the include path used by this Preprocessor. + * Sets the user include path used by this Preprocessor. */ /* Note for future: Create an IncludeHandler? */ - public void setIncludePath(List path) { - this.path = path; + public void setQuoteIncludePath(List path) { + this.quoteincludepath = path; + } + + public List getQuoteIncludePath() { + return quoteincludepath; + } + + /** + * Sets the system include path used by this Preprocessor. + */ + /* Note for future: Create an IncludeHandler? */ + public void setSystemIncludePath(List path) { + this.sysincludepath = path; + } + + public List getSystemIncludePath() { + return sysincludepath; } /** @@ -804,6 +822,28 @@ public class Preprocessor { return source_skipline(true); } + protected boolean include(File file) + throws IOException, + LexerException { + if (!file.exists()) + return false; + if (!file.isFile()) + return false; + push_source(new FileLexerSource(file), true); + return true; + } + + protected boolean include(Iterable path, String name) + throws IOException, + LexerException { + for (String dir : path) { + File file = new File(dir + File.separator + name); + if (include(file)) + return true; + } + return false; + } + /** * Handles a include directive. * @@ -820,27 +860,22 @@ public class Preprocessor { if (dir == null) dir = new File("/"); File file = new File(dir, name); - // System.err.println("Include: " + file); - if (file.exists() && file.isFile()) { - push_source(new FileLexerSource(file), true); + if (include(file)) + return; + if (include(quoteincludepath, name)) return; - } } - if (path != null) { - for (int i = 0; i < path.size(); i++) { - File file = new File( - path.get(i) + File.separator + name - ); - if (file.exists() && file.isFile()) { - // System.err.println("Include: " + file); - push_source(new FileLexerSource(file), true); - return; - } - } - } + if (include(sysincludepath, name)) + return; - error(line, 0, "File not found: " + name + " in " + path); + StringBuilder buf = new StringBuilder(); + if (quoted) + for (String dir : quoteincludepath) + buf.append(" ").append(dir); + for (String dir : sysincludepath) + buf.append(" ").append(dir); + error(line, 0, "File not found: " + name + " in " + buf); } private Token include() diff --git a/src/tests/org/anarres/cpp/CppReaderTestCase.java b/src/tests/org/anarres/cpp/CppReaderTestCase.java index 6097436..92ade21 100644 --- a/src/tests/org/anarres/cpp/CppReaderTestCase.java +++ b/src/tests/org/anarres/cpp/CppReaderTestCase.java @@ -14,7 +14,7 @@ public class CppReaderTestCase extends BaseTestCase implements Test { System.out.println("Testing " + in + " => " + out); StringReader r = new StringReader(in); CppReader p = new CppReader(r); - p.getPreprocessor().setIncludePath( + p.getPreprocessor().setSystemIncludePath( Collections.singletonList("src/input") ); p.getPreprocessor().getFeatures().add(Feature.LINEMARKERS); diff --git a/src/tests/org/anarres/cpp/MainTestCase.java b/src/tests/org/anarres/cpp/MainTestCase.java index a42f3d2..313a463 100644 --- a/src/tests/org/anarres/cpp/MainTestCase.java +++ b/src/tests/org/anarres/cpp/MainTestCase.java @@ -9,7 +9,7 @@ import static org.anarres.cpp.Token.*; public class MainTestCase extends BaseTestCase { public void testMain() throws Exception { - Main.main(new String[] { "-V" }); + Main.main(new String[] { "--version" }); } } -- cgit v1.2.3