# HG changeset patch # User Kaito Tokumori # Date 1429173183 -32400 # Node ID 2ddce554fef0cdeb5553cf8c1aedfafe4fda83ac # Parent 9020ffd06b8bc353ed9bfd0d2d00e038d4345628 fix bug:LLVM/clang doesn't crash when code segment declaration was not found. diff -r 9020ffd06b8b -r 2ddce554fef0 tools/clang/.git/index Binary file tools/clang/.git/index has changed diff -r 9020ffd06b8b -r 2ddce554fef0 tools/clang/include/clang/Parse/Parser.h --- 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(0)); #endif bool isDeclarationSpecifier(bool DisambiguatingWithExpression = false); diff -r 9020ffd06b8b -r 2ddce554fef0 tools/clang/lib/Parse/ParseCbC.cpp --- 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(L) & static_cast(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 diff -r 9020ffd06b8b -r 2ddce554fef0 tools/clang/lib/Parse/ParseDecl.cpp --- 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); } diff -r 9020ffd06b8b -r 2ddce554fef0 tools/clang/lib/Parse/ParseStmt.cpp --- 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;