[cfe-commits] r66148 - in /cfe/trunk: include/clang/Parse/Parser.h lib/Parse/MinimalAction.cpp lib/Parse/Parser.cpp

Chris Lattner sabre at nondot.org
Wed Mar 4 23:24:28 PST 2009


Author: lattner
Date: Thu Mar  5 01:24:28 2009
New Revision: 66148

URL: http://llvm.org/viewvc/llvm-project?rev=66148&view=rev
Log:
When the parser is live, print out the location and spelling of its current token.
For example:

Stack dump:
0.	Program arguments: clang t.cpp 
1.	t.cpp:4:8: current parser token: ';'
2.	t.cpp:3:1: parsing struct/union/class body 'x'
Abort

It is weird that the parser is always "underneath" any parse context 
actions, but the parser is created first.


Modified:
    cfe/trunk/include/clang/Parse/Parser.h
    cfe/trunk/lib/Parse/MinimalAction.cpp
    cfe/trunk/lib/Parse/Parser.cpp

Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=66148&r1=66147&r2=66148&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Thu Mar  5 01:24:28 2009
@@ -25,12 +25,25 @@
   class PragmaHandler;
   class Scope;
   class DiagnosticBuilder;
+  class Parser;
 
+/// PrettyStackTraceParserEntry - If a crash happens while the parser is active,
+/// an entry is printed for it.
+class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
+  const Parser &P;
+public:
+  PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
+  virtual void print(llvm::raw_ostream &OS) const;
+};
+  
+  
 /// Parser - This implements a parser for the C family of languages.  After
 /// parsing units of the grammar, productions are invoked to handle whatever has
 /// been read.
 ///
 class Parser {
+  PrettyStackTraceParserEntry CrashInfo;
+  
   Preprocessor &PP;
   
   /// Tok - The current token we are peeking ahead.  All parsing methods assume
@@ -92,8 +105,11 @@
 
   const LangOptions &getLang() const { return PP.getLangOptions(); }
   TargetInfo &getTargetInfo() const { return PP.getTargetInfo(); }
+  Preprocessor &getPreprocessor() const { return PP; }
   Action &getActions() const { return Actions; }
   
+  const Token &getCurToken() const { return Tok; }
+  
   // Type forwarding.  All of these are statically 'void*', but they may all be
   // different actual classes based on the actions in place.
   typedef Action::ExprTy ExprTy;

Modified: cfe/trunk/lib/Parse/MinimalAction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/MinimalAction.cpp?rev=66148&r1=66147&r2=66148&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/MinimalAction.cpp (original)
+++ cfe/trunk/lib/Parse/MinimalAction.cpp Thu Mar  5 01:24:28 2009
@@ -19,6 +19,29 @@
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;
 
+///  Out-of-line virtual destructor to provide home for Action class.
+ActionBase::~ActionBase() {}
+
+///  Out-of-line virtual destructor to provide home for Action class.
+Action::~Action() {}
+
+// Defined out-of-line here because of dependecy on AttributeList
+Action::DeclTy *Action::ActOnUsingDirective(Scope *CurScope,
+                                            SourceLocation UsingLoc,
+                                            SourceLocation NamespcLoc,
+                                            const CXXScopeSpec &SS,
+                                            SourceLocation IdentLoc,
+                                            IdentifierInfo *NamespcName,
+                                            AttributeList *AttrList) {
+  
+  // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
+  // passed AttributeList, however other actions don't free it, is it
+  // temporary state or bug?
+  delete AttrList;
+  return 0;
+}
+
+
 void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
   if (Loc.isValid()) {
     Loc.print(OS, SM);

Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=66148&r1=66147&r2=66148&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Thu Mar  5 01:24:28 2009
@@ -15,12 +15,13 @@
 #include "clang/Parse/ParseDiagnostic.h"
 #include "clang/Parse/DeclSpec.h"
 #include "clang/Parse/Scope.h"
+#include "llvm/Support/raw_ostream.h"
 #include "ExtensionRAIIObject.h"
 #include "ParsePragma.h"
 using namespace clang;
 
 Parser::Parser(Preprocessor &pp, Action &actions)
-  : PP(pp), Actions(actions), Diags(PP.getDiagnostics()), 
+  : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()), 
     GreaterThanIsOperator(true) {
   Tok.setKind(tok::eof);
   CurScope = 0;
@@ -38,30 +39,23 @@
   PushTopClassStack();
 }
 
-///  Out-of-line virtual destructor to provide home for Action class.
-ActionBase::~ActionBase() {}
-
-///  Out-of-line virtual destructor to provide home for Action class.
-Action::~Action() {}
-
-// Defined out-of-line here because of dependecy on AttributeList
-Action::DeclTy *Action::ActOnUsingDirective(Scope *CurScope,
-                                            SourceLocation UsingLoc,
-                                            SourceLocation NamespcLoc,
-                                            const CXXScopeSpec &SS,
-                                            SourceLocation IdentLoc,
-                                            IdentifierInfo *NamespcName,
-                                            AttributeList *AttrList) {
-
-  // FIXME: Parser seems to assume that Action::ActOn* takes ownership over
-  // passed AttributeList, however other actions don't free it, is it
-  // temporary state or bug?
-  delete AttrList;
-  return 0;
+/// If a crash happens while the parser is active, print out a line indicating
+/// what the current token is.
+void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const {
+  const Token &Tok = P.getCurToken();
+  if (Tok.getLocation().isInvalid()) {
+    OS << "<eof> parser at end of file\n";
+    return;
+  }
+  
+  const Preprocessor &PP = P.getPreprocessor();
+  Tok.getLocation().print(OS, PP.getSourceManager());
+  OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
 }
 
+
 DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
-  return Diags.Report(FullSourceLoc(Loc,PP.getSourceManager()), DiagID);
+  return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID);
 }
 
 DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {





More information about the cfe-commits mailing list