From: Chris Lattner Date: Sun, 2 Dec 2007 01:13:47 +0000 (+0000) Subject: fix a crash when the rewriter would scan off the beginning of the file. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=26de4655a38b899e0f0dcef4175032175854d1cf;p=clang fix a crash when the rewriter would scan off the beginning of the file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44499 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index 2073f7e391..b199f7151f 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -18,9 +18,10 @@ #include "clang/Basic/SourceManager.h" #include "clang/Basic/IdentifierTable.h" #include "clang/Basic/Diagnostic.h" +#include "clang/Lex/Lexer.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/SmallPtrSet.h" -#include "clang/Lex/Lexer.h" +#include "llvm/Support/MemoryBuffer.h" #include using namespace clang; using llvm::utostr; @@ -32,6 +33,7 @@ namespace { ASTContext *Context; SourceManager *SM; unsigned MainFileID; + const char *MainFileStart, *MainFileEnd; SourceLocation LastIncLoc; llvm::SmallVector ClassImplementation; llvm::SmallVector CategoryImplementation; @@ -58,7 +60,6 @@ namespace { void Initialize(ASTContext &context, unsigned mainFileID) { Context = &context; SM = &Context->SourceMgr; - MainFileID = mainFileID; MsgSendFunctionDecl = 0; MsgSendSuperFunctionDecl = 0; GetClassFunctionDecl = 0; @@ -69,6 +70,13 @@ namespace { CurMethodDecl = 0; SuperStructDecl = 0; + // Get the ID and start/end of the main file. + MainFileID = mainFileID; + const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); + MainFileStart = MainBuf->getBufferStart(); + MainFileEnd = MainBuf->getBufferEnd(); + + Rewrite.setSourceMgr(Context->SourceMgr); // declaring objc_selector outside the parameter list removes a silly // scope related warning... @@ -952,7 +960,7 @@ void RewriteTest::RewriteObjcQualifiedInterfaceTypes( const char *endBuf = SM->getCharacterData(Loc); const char *startBuf = endBuf; - while (*startBuf != ';') + while (*startBuf != ';' && startBuf != MainFileStart) startBuf--; // scan backward (from the decl location) for return type. const char *startRef = 0, *endRef = 0; if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {