From 781f943ec1c021ce4eebe4a549079a48cb8526de Mon Sep 17 00:00:00 2001 From: ishmal Date: Sat, 3 Mar 2007 22:31:09 +0000 Subject: [PATCH] improve error messages --- buildtool.cpp | 136 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 99 insertions(+), 37 deletions(-) diff --git a/buildtool.cpp b/buildtool.cpp index 458fca5a4..830d3f05d 100644 --- a/buildtool.cpp +++ b/buildtool.cpp @@ -37,7 +37,7 @@ * */ -#define BUILDTOOL_VERSION "BuildTool v0.6.4, 2007 Bob Jamison" +#define BUILDTOOL_VERSION "BuildTool v0.6.5, 2007 Bob Jamison" #include #include @@ -960,18 +960,18 @@ friend class Parser; public: Element() { - parent = NULL; + init(); } Element(const String &nameArg) { - parent = NULL; + init(); name = nameArg; } Element(const String &nameArg, const String &valueArg) { - parent = NULL; + init(); name = nameArg; value = valueArg; } @@ -1039,9 +1039,18 @@ public: * @param elem the element to output */ void print(); + + int getLine() + { return line; } protected: + void init() + { + parent = NULL; + line = 0; + } + void assign(const Element &other) { parent = other.parent; @@ -1050,6 +1059,7 @@ protected: namespaces = other.namespaces; name = other.name; value = other.value; + line = other.line; } void findElementsRecursive(std::vector&res, const String &name); @@ -1065,7 +1075,8 @@ protected: String name; String value; - + + int line; }; @@ -1133,15 +1144,17 @@ private: currentPosition = 0; } - void getLineAndColumn(long pos, long *lineNr, long *colNr); + int countLines(int begin, int end); + + void getLineAndColumn(int pos, int *lineNr, int *colNr); void error(char *fmt, ...); - int peek(long pos); + int peek(int pos); - int match(long pos, const char *text); + int match(int pos, const char *text); - int skipwhite(long p); + int skipwhite(int p); int getWord(int p0, String &buf); @@ -1157,12 +1170,10 @@ private: bool keepGoing; Element *currentNode; - long parselen; + int parselen; XMLCh *parsebuf; - String cdatabuf; - long currentPosition; - int colNr; - + String cdatabuf; + int currentPosition; }; @@ -1178,6 +1189,7 @@ Element *Element::clone() elem->parent = parent; elem->attributes = attributes; elem->namespaces = namespaces; + elem->line = line; std::vector::iterator iter; for (iter = children.begin(); iter != children.end() ; iter++) @@ -1356,10 +1368,24 @@ String Parser::trim(const String &s) return res; } -void Parser::getLineAndColumn(long pos, long *lineNr, long *colNr) + +int Parser::countLines(int begin, int end) { - long line = 1; - long col = 1; + int count = 0; + for (int i=begin ; i= parselen) return -1; @@ -1428,7 +1454,7 @@ String Parser::encode(const String &str) } -int Parser::match(long p0, const char *text) +int Parser::match(int p0, const char *text) { int p = p0; while (*text) @@ -1442,7 +1468,7 @@ int Parser::match(long p0, const char *text) -int Parser::skipwhite(long p) +int Parser::skipwhite(int p) { while (pline = lineNr + countLines(p0, p); n->parent = par; par->addChild(n); @@ -1717,7 +1749,7 @@ int Parser::parseElement(int p0, Element *par,int depth) //# CHILD ELEMENT if (ch == '<') { - p2 = parseElement(p, n, depth+1); + p2 = parseElement(p, n, lineNr + countLines(p0, p)); if (p2 == p) { /* @@ -1809,7 +1841,7 @@ Element *Parser::parse(XMLCh *buf,int pos,int len) Element *rootNode = new Element("root"); pos = parseVersion(pos); pos = parseDoctype(pos); - pos = parseElement(pos, rootNode, 0); + pos = parseElement(pos, rootNode, 1); return rootNode; } @@ -2839,8 +2871,9 @@ private: class MakeBase { public: + MakeBase() - {} + { line = 0; } virtual ~MakeBase() {} @@ -2870,6 +2903,18 @@ public: * Get an element value, performing substitutions if necessary */ bool getValue(Element *elem, String &result); + + /** + * Set the current line number in the file + */ + void setLine(int val) + { line = val; } + + /** + * Get the current line number in the file + */ + int getLine() + { return line; } protected: @@ -3042,6 +3087,7 @@ private: */ bool getSubstitutions(const String &s, String &result); + int line; }; @@ -3056,7 +3102,7 @@ void MakeBase::error(char *fmt, ...) { va_list args; va_start(args,fmt); - fprintf(stderr, "Make error: "); + fprintf(stderr, "Make error line %d: ", line); vfprintf(stderr, fmt, args); fprintf(stderr, "\n"); va_end(args) ; @@ -5727,7 +5773,7 @@ public: /** * */ - Task *createTask(Element *elem); + Task *createTask(Element *elem, int lineNr); protected: @@ -5943,7 +5989,10 @@ public: //## Execute the command String outString, errString; + trace("BEFORE"); bool ret = executeCommand(cmd.c_str(), "", outString, errString); + trace("AFTER"); + if (f) { fprintf(f, "########################### File : %s\n", @@ -6366,7 +6415,12 @@ public: delType = DEL_DIR; if (fileName.size()>0 && dirName.size()>0) { - error(" can only have one attribute of file= or dir="); + error(" can have one attribute of file= or dir="); + return false; + } + if (fileName.size()==0 && dirName.size()==0) + { + error(" must have one attribute of file= or dir="); return false; } String ret; @@ -7377,7 +7431,7 @@ public: /** * */ -Task *Task::createTask(Element *elem) +Task *Task::createTask(Element *elem, int lineNr) { String tagName = elem->getName(); //trace("task:%s", tagName.c_str()); @@ -7420,6 +7474,8 @@ Task *Task::createTask(Element *elem) return NULL; } + task->setLine(lineNr); + if (!task->parse(elem)) { delete task; @@ -7716,11 +7772,6 @@ private: */ bool parseProperty(Element *elem); - /** - * - */ - bool parseTask(Task &task, Element *elem); - /** * */ @@ -8179,6 +8230,8 @@ bool Make::parseFile() { status("######## PARSE : %s", uri.getPath().c_str()); + setLine(0); + Parser parser; Element *root = parser.parseFile(uri.getNativePath()); if (!root) @@ -8187,6 +8240,8 @@ bool Make::parseFile() uri.getNativePath().c_str()); return false; } + + setLine(root->getLine()); if (root->getChildren().size()==0 || root->getChildren()[0]->getName()!="project") @@ -8213,6 +8268,7 @@ bool Make::parseFile() for (unsigned int i=0 ; igetLine()); String tagName = elem->getName(); //########## DESCRIPTION @@ -8247,7 +8303,7 @@ bool Make::parseFile() { Element *telem = telems[i]; Task breeder(*this); - Task *task = breeder.createTask(telem); + Task *task = breeder.createTask(telem, telem->getLine()); if (!task) return false; allTasks.push_back(task); @@ -8269,6 +8325,12 @@ bool Make::parseFile() //more work than targets[tname]=target, but avoids default allocator targets.insert(std::make_pair(tname, target)); } + //######### none of the above + else + { + error("unknown toplevel tag: <%s>", tagName.c_str()); + return false; + } } -- 2.30.2