From 88c2596edc8eb475e20f6033de1ea01669695a0c Mon Sep 17 00:00:00 2001
From: Argyrios Kyrtzidis <akyrtzi@gmail.com>
Date: Fri, 18 Nov 2011 00:26:59 +0000
Subject: [PATCH] Change ASTConsumer::HandleTopLevelDecl to return true for the
 parser to continue parsing or false to abort parsing.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@144943 91177308-0d34-0410-b5e6-96231b3b80d8
---
 include/clang/AST/ASTConsumer.h            |  4 +++-
 include/clang/Frontend/MultiplexConsumer.h |  2 +-
 lib/AST/ASTConsumer.cpp                    |  4 +++-
 lib/CodeGen/CodeGenAction.cpp              |  4 +++-
 lib/CodeGen/ModuleBuilder.cpp              |  3 ++-
 lib/Frontend/ASTConsumers.cpp              |  3 ++-
 lib/Frontend/ASTUnit.cpp                   |  6 ++++--
 lib/Frontend/MultiplexConsumer.cpp         |  3 ++-
 lib/Parse/ParseAST.cpp                     | 13 +++++++++++--
 lib/Rewrite/RewriteObjC.cpp                |  3 ++-
 tools/libclang/Indexing.cpp                |  7 ++-----
 11 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/include/clang/AST/ASTConsumer.h b/include/clang/AST/ASTConsumer.h
index c6cb71cc7c5..c1cde7f3ce6 100644
--- a/include/clang/AST/ASTConsumer.h
+++ b/include/clang/AST/ASTConsumer.h
@@ -48,7 +48,9 @@ public:
   /// called by the parser to process every top-level Decl*. Note that D can be
   /// the head of a chain of Decls (e.g. for `int a, b` the chain will have two
   /// elements). Use Decl::getNextDeclarator() to walk the chain.
-  virtual void HandleTopLevelDecl(DeclGroupRef D);
+  ///
+  /// \returns true to continue parsing, or false to abort parsing.
+  virtual bool HandleTopLevelDecl(DeclGroupRef D);
 
   /// HandleInterestingDecl - Handle the specified interesting declaration. This
   /// is called by the AST reader when deserializing things that might interest
diff --git a/include/clang/Frontend/MultiplexConsumer.h b/include/clang/Frontend/MultiplexConsumer.h
index 4242f011714..89972991d18 100644
--- a/include/clang/Frontend/MultiplexConsumer.h
+++ b/include/clang/Frontend/MultiplexConsumer.h
@@ -33,7 +33,7 @@ public:
 
   // ASTConsumer
   virtual void Initialize(ASTContext &Context);
-  virtual void HandleTopLevelDecl(DeclGroupRef D);
+  virtual bool HandleTopLevelDecl(DeclGroupRef D);
   virtual void HandleInterestingDecl(DeclGroupRef D);
   virtual void HandleTranslationUnit(ASTContext &Ctx);
   virtual void HandleTagDeclDefinition(TagDecl *D);
diff --git a/lib/AST/ASTConsumer.cpp b/lib/AST/ASTConsumer.cpp
index 062f1103e91..1672bc8aed7 100644
--- a/lib/AST/ASTConsumer.cpp
+++ b/lib/AST/ASTConsumer.cpp
@@ -15,7 +15,9 @@
 #include "clang/AST/DeclGroup.h"
 using namespace clang;
 
-void ASTConsumer::HandleTopLevelDecl(DeclGroupRef D) {}
+bool ASTConsumer::HandleTopLevelDecl(DeclGroupRef D) {
+  return true;
+}
 
 void ASTConsumer::HandleInterestingDecl(DeclGroupRef D) {
   HandleTopLevelDecl(D);
diff --git a/lib/CodeGen/CodeGenAction.cpp b/lib/CodeGen/CodeGenAction.cpp
index db2bab9bf61..fb926e1e8e6 100644
--- a/lib/CodeGen/CodeGenAction.cpp
+++ b/lib/CodeGen/CodeGenAction.cpp
@@ -86,7 +86,7 @@ namespace clang {
         LLVMIRGeneration.stopTimer();
     }
 
-    virtual void HandleTopLevelDecl(DeclGroupRef D) {
+    virtual bool HandleTopLevelDecl(DeclGroupRef D) {
       PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
                                      Context->getSourceManager(),
                                      "LLVM IR generation of declaration");
@@ -98,6 +98,8 @@ namespace clang {
 
       if (llvm::TimePassesIsEnabled)
         LLVMIRGeneration.stopTimer();
+
+      return true;
     }
 
     virtual void HandleTranslationUnit(ASTContext &C) {
diff --git a/lib/CodeGen/ModuleBuilder.cpp b/lib/CodeGen/ModuleBuilder.cpp
index 793ee9192e6..30572ed9af2 100644
--- a/lib/CodeGen/ModuleBuilder.cpp
+++ b/lib/CodeGen/ModuleBuilder.cpp
@@ -59,10 +59,11 @@ namespace {
                                                *M, *TD, Diags));
     }
 
-    virtual void HandleTopLevelDecl(DeclGroupRef DG) {
+    virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
       // Make sure to emit all elements of a Decl.
       for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
         Builder->EmitTopLevelDecl(*I);
+      return true;
     }
 
     /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
diff --git a/lib/Frontend/ASTConsumers.cpp b/lib/Frontend/ASTConsumers.cpp
index 54bb2827286..390ae09497b 100644
--- a/lib/Frontend/ASTConsumers.cpp
+++ b/lib/Frontend/ASTConsumers.cpp
@@ -66,9 +66,10 @@ namespace {
       this->Context = &Context;
     }
 
-    virtual void HandleTopLevelDecl(DeclGroupRef D) {
+    virtual bool HandleTopLevelDecl(DeclGroupRef D) {
       for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
         HandleTopLevelSingleDecl(*I);
+      return true;
     }
 
     void HandleTopLevelSingleDecl(Decl *D);
diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp
index c59f2d16d28..6cd07842157 100644
--- a/lib/Frontend/ASTUnit.cpp
+++ b/lib/Frontend/ASTUnit.cpp
@@ -878,9 +878,10 @@ public:
     }
   }
 
-  void HandleTopLevelDecl(DeclGroupRef D) {
+  bool HandleTopLevelDecl(DeclGroupRef D) {
     for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
       handleTopLevelDecl(*it);
+    return true;
   }
 
   // We're not interested in "interesting" decls.
@@ -926,7 +927,7 @@ public:
     Hash = 0;
   }
 
-  virtual void HandleTopLevelDecl(DeclGroupRef D) {
+  virtual bool HandleTopLevelDecl(DeclGroupRef D) {
     for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
       Decl *D = *it;
       // FIXME: Currently ObjC method declarations are incorrectly being
@@ -938,6 +939,7 @@ public:
       AddTopLevelDeclarationToHash(D, Hash);
       TopLevelDecls.push_back(D);
     }
+    return true;
   }
 
   virtual void HandleTranslationUnit(ASTContext &Ctx) {
diff --git a/lib/Frontend/MultiplexConsumer.cpp b/lib/Frontend/MultiplexConsumer.cpp
index 8e746f65a90..e7b886ccb60 100644
--- a/lib/Frontend/MultiplexConsumer.cpp
+++ b/lib/Frontend/MultiplexConsumer.cpp
@@ -183,9 +183,10 @@ void MultiplexConsumer::Initialize(ASTContext &Context) {
     Consumers[i]->Initialize(Context);
 }
 
-void MultiplexConsumer::HandleTopLevelDecl(DeclGroupRef D) {
+bool MultiplexConsumer::HandleTopLevelDecl(DeclGroupRef D) {
   for (size_t i = 0, e = Consumers.size(); i != e; ++i)
     Consumers[i]->HandleTopLevelDecl(D);
+  return true;
 }
 
 void MultiplexConsumer::HandleInterestingDecl(DeclGroupRef D) {
diff --git a/lib/Parse/ParseAST.cpp b/lib/Parse/ParseAST.cpp
index fdd7d0f151a..a5c345afe29 100644
--- a/lib/Parse/ParseAST.cpp
+++ b/lib/Parse/ParseAST.cpp
@@ -79,15 +79,24 @@ void clang::ParseAST(Sema &S, bool PrintStats) {
   if (ExternalASTSource *External = S.getASTContext().getExternalSource())
     External->StartTranslationUnit(Consumer);
   
+  bool Abort = false;
   Parser::DeclGroupPtrTy ADecl;
   
   while (!P.ParseTopLevelDecl(ADecl)) {  // Not end of file.
     // If we got a null return and something *was* parsed, ignore it.  This
     // is due to a top-level semicolon, an action override, or a parse error
     // skipping something.
-    if (ADecl)
-      Consumer->HandleTopLevelDecl(ADecl.get());
+    if (ADecl) {
+      if (!Consumer->HandleTopLevelDecl(ADecl.get())) {
+        Abort = true;
+        break;
+      }
+    }
   };
+
+  if (Abort)
+    return;
+
   // Check for any pending objective-c implementation decl.
   while ((ADecl = P.FinishPendingObjCActions()))
     Consumer->HandleTopLevelDecl(ADecl.get());
diff --git a/lib/Rewrite/RewriteObjC.cpp b/lib/Rewrite/RewriteObjC.cpp
index 372e750132f..f02e77e92b7 100644
--- a/lib/Rewrite/RewriteObjC.cpp
+++ b/lib/Rewrite/RewriteObjC.cpp
@@ -167,7 +167,7 @@ namespace {
     virtual void Initialize(ASTContext &context);
 
     // Top Level Driver code.
-    virtual void HandleTopLevelDecl(DeclGroupRef D) {
+    virtual bool HandleTopLevelDecl(DeclGroupRef D) {
       for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
         if (isa<ObjCClassDecl>((*I))) {
           RewriteForwardClassDecl(D);
@@ -175,6 +175,7 @@ namespace {
         }
         HandleTopLevelSingleDecl(*I);
       }
+      return true;
     }
     void HandleTopLevelSingleDecl(Decl *D);
     void HandleDeclInMainFile(Decl *D);
diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp
index 5858faad2e0..3fa5a20ffcf 100644
--- a/tools/libclang/Indexing.cpp
+++ b/tools/libclang/Indexing.cpp
@@ -115,12 +115,9 @@ public:
   virtual void HandleTranslationUnit(ASTContext &Ctx) {
   }
 
-  virtual void HandleTopLevelDecl(DeclGroupRef DG) {
+  virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
     IndexCtx.indexDeclGroupRef(DG);
-    // FIXME: Indicate to parser to abort.
-//    if (IndexCtx.shouldAbort()) {
-//      
-//    }
+    return !IndexCtx.shouldAbort();
   }
 
   /// \brief Handle the specified top-level declaration that occurred inside
-- 
GitLab