changeset 90:2ddce554fef0

fix bug:LLVM/clang doesn't crash when code segment declaration was not found.
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Thu, 16 Apr 2015 17:33:03 +0900
parents 9020ffd06b8b
children ae2ab28b985c
files tools/clang/.git/index tools/clang/include/clang/Parse/Parser.h tools/clang/lib/Parse/ParseCbC.cpp tools/clang/lib/Parse/ParseDecl.cpp tools/clang/lib/Parse/ParseStmt.cpp
diffstat 5 files changed, 50 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
Binary file tools/clang/.git/index has changed
--- a/tools/clang/include/clang/Parse/Parser.h	Tue Apr 14 03:55:39 2015 +0900
+++ b/tools/clang/include/clang/Parse/Parser.h	Thu Apr 16 17:33:03 2015 +0900
@@ -1806,6 +1806,7 @@
   bool isVoidFunction();
   bool SearchCodeSegmentDeclaration(std::string Name);
   void CreatePrototypeDeclaration(Token IITok);
+  bool SkipAnyUntil(tok::TokenKind T, SkipUntilFlags Flags = static_cast<SkipUntilFlags>(0));
 #endif
 
   bool isDeclarationSpecifier(bool DisambiguatingWithExpression = false);
--- a/tools/clang/lib/Parse/ParseCbC.cpp	Tue Apr 14 03:55:39 2015 +0900
+++ b/tools/clang/lib/Parse/ParseCbC.cpp	Thu Apr 16 17:33:03 2015 +0900
@@ -930,7 +930,7 @@
 /// SearchCodeSegmentDeclaration - Read tokens until we get to the specified code segment declaration.
 /// If we can't find it , return false;
 bool Parser::SearchCodeSegmentDeclaration(std::string Name){
-  while(SkipUntil(tok::kw___code,StopBeforeMatch)){
+  while(SkipAnyUntil(tok::kw___code, StopBeforeMatch)){
     if(NextToken().is(tok::identifier) && NextToken().getIdentifierInfo()->getName().str() == Name)
       return true;
     ConsumeToken();
@@ -950,32 +950,69 @@
     TopScope = TopScope->getParent();
   Actions.CurScope = TopScope;
   
-  Token CachedTokens[] = {IITok, PP.LookAhead(1)};
+  Token CachedTokens[3] = {IITok, PP.LookAhead(1)};
   Token SavedToken = Tok;
   PP.ClearCache();
   ProtoParsing = true;
   
-  StringRef Filename;
-  Filename = StringRef("proto.h",7);
   const DirectoryLookup *CurDir;
   FileID FID = PP.getSourceManager().createFileID(PP.getCurrentFileLexer()->getFileEntry(), IITok.getLocation(), SrcMgr::C_User);
   PP.EnterSourceFile(FID,CurDir,IITok.getLocation());
   ConsumeToken();
-  SearchCodeSegmentDeclaration(IITok.getIdentifierInfo()->getName().str());
-  
+
+  if(SearchCodeSegmentDeclaration(IITok.getIdentifierInfo()->getName().str())){
+    DeclGroupPtrTy ProtoDecl;
+    ParseTopLevelDecl(ProtoDecl);
+    // add declaration to AST.
+    if(ProtoDecl)
+      (&Actions.getASTConsumer())->HandleTopLevelDecl(ProtoDecl.get());
+    // File Closing
+    Token T;
+    PP.HandleEndOfFile(T, false);
+
+    // recover tokens.
+    Tok = SavedToken;
+    PP.RestoreTokens(CachedTokens, 2);
+    
+  }
+  else {
+    // recover tokens.
+    CachedTokens[2] = Tok;
+    Tok = SavedToken;
+    PP.RestoreTokens(CachedTokens, 3);
+  }
+
   // move to the previous scope.
-  DeclGroupPtrTy ProtoDecl;
-  ParseTopLevelDecl(ProtoDecl);
-  (&Actions.getASTConsumer())->HandleTopLevelDecl(ProtoDecl.get());
   Actions.CurScope = SavedScope;
   Actions.CurContext = SavedContext;
   Actions.FunctionScopes.push_back(SavedFSI);
   
-  // recover tokens
-  Tok = SavedToken;
-  PP.RestoreTokens(CachedTokens, 2);
+
   
   ProtoParsing = false;
 }
 
+static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) {
+  return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0;
+}
+
+bool Parser::SkipAnyUntil(tok::TokenKind T, SkipUntilFlags Flags){
+  const PreprocessorLexer *L = PP.getCurrentLexer();
+  while(1){
+    if(Tok.is(T)){
+      if (HasFlagsSet(Flags, StopBeforeMatch)) {
+        // Noop, don't consume the token.
+      } else {
+        ConsumeAnyToken();
+      }
+      return true;
+    }
+    else if(!PP.isCurrentLexer(L)){
+      return false;
+    }
+
+    ConsumeAnyToken();
+  }
+}
+
 #endif
--- a/tools/clang/lib/Parse/ParseDecl.cpp	Tue Apr 14 03:55:39 2015 +0900
+++ b/tools/clang/lib/Parse/ParseDecl.cpp	Thu Apr 16 17:33:03 2015 +0900
@@ -1811,12 +1811,6 @@
     }
   }
 
-#ifndef noCbC
-  if(ProtoParsing){
-    Token T;
-    PP.HandleEndOfFile(T, false);
-  }
-#endif
   return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
 }
 
--- a/tools/clang/lib/Parse/ParseStmt.cpp	Tue Apr 14 03:55:39 2015 +0900
+++ b/tools/clang/lib/Parse/ParseStmt.cpp	Thu Apr 16 17:33:03 2015 +0900
@@ -27,8 +27,6 @@
 
 #ifndef noCbC
 #include "clang/Sema/Lookup.h"
-#include "clang/Basic/SourceManager.h"
-#include "clang/AST/ASTConsumer.h"
 #endif
 
 using namespace clang;