[cfe-commits] r44499 - /cfe/trunk/Driver/RewriteTest.cpp

Chris Lattner sabre at nondot.org
Sat Dec 1 17:13:47 PST 2007


Author: lattner
Date: Sat Dec  1 19:13:47 2007
New Revision: 44499

URL: http://llvm.org/viewvc/llvm-project?rev=44499&view=rev
Log:
fix a crash when the rewriter would scan off the beginning of the file.

Modified:
    cfe/trunk/Driver/RewriteTest.cpp

Modified: cfe/trunk/Driver/RewriteTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteTest.cpp?rev=44499&r1=44498&r2=44499&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteTest.cpp (original)
+++ cfe/trunk/Driver/RewriteTest.cpp Sat Dec  1 19:13:47 2007
@@ -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 <sstream>
 using namespace clang;
 using llvm::utostr;
@@ -32,6 +33,7 @@
     ASTContext *Context;
     SourceManager *SM;
     unsigned MainFileID;
+    const char *MainFileStart, *MainFileEnd;
     SourceLocation LastIncLoc;
     llvm::SmallVector<ObjcImplementationDecl *, 8> ClassImplementation;
     llvm::SmallVector<ObjcCategoryImplDecl *, 8> CategoryImplementation;
@@ -58,7 +60,6 @@
     void Initialize(ASTContext &context, unsigned mainFileID) {
       Context = &context;
       SM = &Context->SourceMgr;
-      MainFileID = mainFileID;
       MsgSendFunctionDecl = 0;
       MsgSendSuperFunctionDecl = 0;
       GetClassFunctionDecl = 0;
@@ -69,6 +70,13 @@
       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 @@
     
     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)) {





More information about the cfe-commits mailing list