From 09381cccffe675ccbc8d183f043788f99dc018cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= Date: Wed, 31 Mar 2010 12:38:31 +0000 Subject: [PATCH] Make source location info in the XML output optional. * src/libexpr/expr-to-xml.cc (nix::showAttrs): Add `location' parameter. Provide location XML attributes when it's true. Update callers. (nix::printTermAsXML): Likewise. * src/libexpr/expr-to-xml.hh (nix::printTermAsXML): Update prototype; have `location' default to `false'. * src/nix-instantiate/nix-instantiate.cc (printResult, processExpr): Add `location' parameter; update callers. (run): Add support for `--no-location'. * src/nix-instantiate/help.txt: Update accordingly. * tests/lang.sh: Invoke `nix-instantiate' with `--no-location' for the XML tests. * tests/lang/eval-okay-toxml.exp, tests/lang/eval-okay-to-xml.nix: New files. --- src/libexpr/expr-to-xml.cc | 22 +++++++++++----------- src/libexpr/expr-to-xml.hh | 2 +- src/nix-instantiate/help.txt | 2 ++ src/nix-instantiate/nix-instantiate.cc | 15 +++++++++------ tests/lang.sh | 3 ++- tests/lang/eval-okay-toxml.exp | 1 + tests/lang/eval-okay-toxml.nix | 3 +++ 7 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 tests/lang/eval-okay-toxml.exp create mode 100644 tests/lang/eval-okay-toxml.nix diff --git a/src/libexpr/expr-to-xml.cc b/src/libexpr/expr-to-xml.cc index 4ca71c889..5857537d7 100644 --- a/src/libexpr/expr-to-xml.cc +++ b/src/libexpr/expr-to-xml.cc @@ -24,11 +24,11 @@ typedef set ExprSet; static void printTermAsXML(Expr e, XMLWriter & doc, PathSet & context, - ExprSet & drvsSeen); + ExprSet & drvsSeen, bool location); static void showAttrs(const ATermMap & attrs, XMLWriter & doc, - PathSet & context, ExprSet & drvsSeen) + PathSet & context, ExprSet & drvsSeen, bool location) { StringSet names; for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i) @@ -43,7 +43,7 @@ static void showAttrs(const ATermMap & attrs, XMLWriter & doc, if(matchAttrRHS(attrRHS, attr, pos)) { ATerm path; int line, column; - if (matchPos(pos, path, line, column)) { + if (location && matchPos(pos, path, line, column)) { xmlAttrs["path"] = aterm2String(path); xmlAttrs["line"] = (format("%1%") % line).str(); xmlAttrs["column"] = (format("%1%") % column).str(); @@ -52,7 +52,7 @@ static void showAttrs(const ATermMap & attrs, XMLWriter & doc, abort(); // Should not happen. XMLOpenElement _(doc, "attr", xmlAttrs); - printTermAsXML(attr, doc, context, drvsSeen); + printTermAsXML(attr, doc, context, drvsSeen, location); } } @@ -83,7 +83,7 @@ static void printPatternAsXML(Pattern pat, XMLWriter & doc) static void printTermAsXML(Expr e, XMLWriter & doc, PathSet & context, - ExprSet & drvsSeen) + ExprSet & drvsSeen, bool location) { XMLAttrs attrs; string s; @@ -137,28 +137,28 @@ static void printTermAsXML(Expr e, XMLWriter & doc, PathSet & context, if (drvsSeen.find(e) == drvsSeen.end()) { drvsSeen.insert(e); - showAttrs(attrs, doc, context, drvsSeen); + showAttrs(attrs, doc, context, drvsSeen, location); } else doc.writeEmptyElement("repeated"); } else { XMLOpenElement _(doc, "attrs"); - showAttrs(attrs, doc, context, drvsSeen); + showAttrs(attrs, doc, context, drvsSeen, location); } } else if (matchList(e, es)) { XMLOpenElement _(doc, "list"); for (ATermIterator i(es); i; ++i) - printTermAsXML(*i, doc, context, drvsSeen); + printTermAsXML(*i, doc, context, drvsSeen, location); } else if (matchFunction(e, pat, body, pos)) { ATerm path; int line, column; XMLAttrs xmlAttrs; - if (matchPos(pos, path, line, column)) { + if (location && matchPos(pos, path, line, column)) { xmlAttrs["path"] = aterm2String(path); xmlAttrs["line"] = (format("%1%") % line).str(); xmlAttrs["column"] = (format("%1%") % column).str(); @@ -172,12 +172,12 @@ static void printTermAsXML(Expr e, XMLWriter & doc, PathSet & context, } -void printTermAsXML(Expr e, std::ostream & out, PathSet & context) +void printTermAsXML(Expr e, std::ostream & out, PathSet & context, bool location) { XMLWriter doc(true, out); XMLOpenElement root(doc, "expr"); ExprSet drvsSeen; - printTermAsXML(e, doc, context, drvsSeen); + printTermAsXML(e, doc, context, drvsSeen, location); } diff --git a/src/libexpr/expr-to-xml.hh b/src/libexpr/expr-to-xml.hh index 36b8e4042..de9d55f32 100644 --- a/src/libexpr/expr-to-xml.hh +++ b/src/libexpr/expr-to-xml.hh @@ -9,7 +9,7 @@ namespace nix { -void printTermAsXML(Expr e, std::ostream & out, PathSet & context); +void printTermAsXML(Expr e, std::ostream & out, PathSet & context, bool location = false); } diff --git a/src/nix-instantiate/help.txt b/src/nix-instantiate/help.txt index fa0a4590e..21822132a 100644 --- a/src/nix-instantiate/help.txt +++ b/src/nix-instantiate/help.txt @@ -22,6 +22,8 @@ Options: For --eval-only / --parse-only: --xml: print an XML representation of the abstract syntax tree + --no-location: don't provide source location information in the + output XML tree For --eval-only: diff --git a/src/nix-instantiate/nix-instantiate.cc b/src/nix-instantiate/nix-instantiate.cc index 3822de5c6..21f352f25 100644 --- a/src/nix-instantiate/nix-instantiate.cc +++ b/src/nix-instantiate/nix-instantiate.cc @@ -38,13 +38,13 @@ static bool indirectRoot = false; static void printResult(EvalState & state, Expr e, - bool evalOnly, bool xmlOutput, const ATermMap & autoArgs) + bool evalOnly, bool xmlOutput, bool location, const ATermMap & autoArgs) { PathSet context; if (evalOnly) if (xmlOutput) - printTermAsXML(e, std::cout, context); + printTermAsXML(e, std::cout, context, location); else std::cout << format("%1%\n") % canonicaliseExpr(e); @@ -67,7 +67,7 @@ static void printResult(EvalState & state, Expr e, void processExpr(EvalState & state, const Strings & attrPaths, bool parseOnly, bool strict, const ATermMap & autoArgs, - bool evalOnly, bool xmlOutput, Expr e) + bool evalOnly, bool xmlOutput, bool location, Expr e) { for (Strings::const_iterator i = attrPaths.begin(); i != attrPaths.end(); ++i) { Expr e2 = findAlongAttrPath(state, *i, autoArgs, e); @@ -76,7 +76,7 @@ void processExpr(EvalState & state, const Strings & attrPaths, e2 = strictEvalExpr(state, e2); else e2 = evalExpr(state, e2); - printResult(state, e2, evalOnly, xmlOutput, autoArgs); + printResult(state, e2, evalOnly, xmlOutput, location, autoArgs); } } @@ -89,6 +89,7 @@ void run(Strings args) bool evalOnly = false; bool parseOnly = false; bool xmlOutput = false; + bool xmlOutputSourceLocation = true; bool strict = false; Strings attrPaths; ATermMap autoArgs(128); @@ -124,6 +125,8 @@ void run(Strings args) indirectRoot = true; else if (arg == "--xml") xmlOutput = true; + else if (arg == "--no-location") + xmlOutputSourceLocation = false; else if (arg == "--strict") strict = true; else if (arg[0] == '-') @@ -139,7 +142,7 @@ void run(Strings args) if (readStdin) { Expr e = parseStdin(state); processExpr(state, attrPaths, parseOnly, strict, autoArgs, - evalOnly, xmlOutput, e); + evalOnly, xmlOutput, xmlOutputSourceLocation, e); } for (Strings::iterator i = files.begin(); @@ -148,7 +151,7 @@ void run(Strings args) Path path = absPath(*i); Expr e = parseExprFromFile(state, path); processExpr(state, attrPaths, parseOnly, strict, autoArgs, - evalOnly, xmlOutput, e); + evalOnly, xmlOutput, xmlOutputSourceLocation, e); } printEvalStats(state); diff --git a/tests/lang.sh b/tests/lang.sh index 18eb0278c..54f010763 100644 --- a/tests/lang.sh +++ b/tests/lang.sh @@ -54,7 +54,8 @@ for i in lang/eval-okay-*.nix; do fi if test -e lang/$i.exp.xml; then - if ! $nixinstantiate --eval-only --xml --strict lang/$i.nix > lang/$i.out.xml; then + if ! $nixinstantiate --eval-only --xml --no-location --strict \ + lang/$i.nix > lang/$i.out.xml; then echo "FAIL: $i should evaluate" fail=1 elif ! cmp -s lang/$i.out.xml lang/$i.exp.xml; then diff --git a/tests/lang/eval-okay-toxml.exp b/tests/lang/eval-okay-toxml.exp new file mode 100644 index 000000000..379f3c076 --- /dev/null +++ b/tests/lang/eval-okay-toxml.exp @@ -0,0 +1 @@ +Str("\n\n \n \n \n \n \n\n",[]) diff --git a/tests/lang/eval-okay-toxml.nix b/tests/lang/eval-okay-toxml.nix new file mode 100644 index 000000000..068c97a6c --- /dev/null +++ b/tests/lang/eval-okay-toxml.nix @@ -0,0 +1,3 @@ +# Make sure the expected XML output is produced; in particular, make sure it +# doesn't contain source location information. +builtins.toXML { a = "s"; }