[llvm-branch-commits] [cfe-branch] r119261 - in /cfe/branches/Apple/whitney: include/clang/Basic/SourceManager.h lib/AST/StmtDumper.cpp lib/AST/TypePrinter.cpp lib/Basic/SourceLocation.cpp lib/Checker/AnalysisConsumer.cpp lib/Checker/AnalyzerStatsChecker.cpp lib/Frontend/DocumentXML.cpp lib/Frontend/PrintPreprocessedOutput.cpp lib/Frontend/TextDiagnosticPrinter.cpp lib/Lex/PPDirectives.cpp lib/Lex/PPMacroExpansion.cpp lib/Lex/Pragma.cpp tools/libclang/CIndexInclusionStack.cpp

Daniel Dunbar daniel at zuster.org
Mon Nov 15 13:46:47 PST 2010


Author: ddunbar
Date: Mon Nov 15 15:46:47 2010
New Revision: 119261

URL: http://llvm.org/viewvc/llvm-project?rev=119261&view=rev
Log:
Merge r118885:
--
Author: Douglas Gregor <dgregor at apple.com>
Date:   Fri Nov 12 07:15:47 2010 +0000

    Make sure to always check the result of
    SourceManager::getPresumedLoc(), so that we don't try to make use of
    an invalid presumed location. Doing so can cause crashes.

Modified:
    cfe/branches/Apple/whitney/include/clang/Basic/SourceManager.h
    cfe/branches/Apple/whitney/lib/AST/StmtDumper.cpp
    cfe/branches/Apple/whitney/lib/AST/TypePrinter.cpp
    cfe/branches/Apple/whitney/lib/Basic/SourceLocation.cpp
    cfe/branches/Apple/whitney/lib/Checker/AnalysisConsumer.cpp
    cfe/branches/Apple/whitney/lib/Checker/AnalyzerStatsChecker.cpp
    cfe/branches/Apple/whitney/lib/Frontend/DocumentXML.cpp
    cfe/branches/Apple/whitney/lib/Frontend/PrintPreprocessedOutput.cpp
    cfe/branches/Apple/whitney/lib/Frontend/TextDiagnosticPrinter.cpp
    cfe/branches/Apple/whitney/lib/Lex/PPDirectives.cpp
    cfe/branches/Apple/whitney/lib/Lex/PPMacroExpansion.cpp
    cfe/branches/Apple/whitney/lib/Lex/Pragma.cpp
    cfe/branches/Apple/whitney/tools/libclang/CIndexInclusionStack.cpp

Modified: cfe/branches/Apple/whitney/include/clang/Basic/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/include/clang/Basic/SourceManager.h?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/include/clang/Basic/SourceManager.h (original)
+++ cfe/branches/Apple/whitney/include/clang/Basic/SourceManager.h Mon Nov 15 15:46:47 2010
@@ -724,6 +724,11 @@
   ///
   /// Note that a presumed location is always given as the instantiation point
   /// of an instantiation location, not at the spelling location.
+  ///
+  /// \returns The presumed location of the specified SourceLocation. If the
+  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
+  /// or the file containing \p Loc has changed on disk), returns an invalid
+  /// presumed location.
   PresumedLoc getPresumedLoc(SourceLocation Loc) const;
 
   /// isFromSameFile - Returns true if both SourceLocations correspond to

Modified: cfe/branches/Apple/whitney/lib/AST/StmtDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/AST/StmtDumper.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/AST/StmtDumper.cpp (original)
+++ cfe/branches/Apple/whitney/lib/AST/StmtDumper.cpp Mon Nov 15 15:46:47 2010
@@ -169,15 +169,15 @@
 void StmtDumper::DumpLocation(SourceLocation Loc) {
   SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
 
-  if (SpellingLoc.isInvalid()) {
-    OS << "<invalid sloc>";
-    return;
-  }
-
   // The general format we print out is filename:line:col, but we drop pieces
   // that haven't changed since the last loc printed.
   PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
 
+  if (PLoc.isInvalid()) {
+    OS << "<invalid sloc>";
+    return;
+  }
+
   if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
     OS << PLoc.getFilename() << ':' << PLoc.getLine()
        << ':' << PLoc.getColumn();

Modified: cfe/branches/Apple/whitney/lib/AST/TypePrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/AST/TypePrinter.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/AST/TypePrinter.cpp (original)
+++ cfe/branches/Apple/whitney/lib/AST/TypePrinter.cpp Mon Nov 15 15:46:47 2010
@@ -481,9 +481,9 @@
       if (!HasKindDecoration)
         OS << " " << D->getKindName();
 
-      if (D->getLocation().isValid()) {
-        PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
+      PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
           D->getLocation());
+      if (PLoc.isValid()) {
         OS << " at " << PLoc.getFilename()
            << ':' << PLoc.getLine()
            << ':' << PLoc.getColumn();

Modified: cfe/branches/Apple/whitney/lib/Basic/SourceLocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Basic/SourceLocation.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Basic/SourceLocation.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Basic/SourceLocation.cpp Mon Nov 15 15:46:47 2010
@@ -43,6 +43,11 @@
 
   if (isFileID()) {
     PresumedLoc PLoc = SM.getPresumedLoc(*this);
+    
+    if (PLoc.isInvalid()) {
+      OS << "<invalid>";
+      return;
+    }
     // The instantiation and spelling pos is identical for file locs.
     OS << PLoc.getFilename() << ':' << PLoc.getLine()
        << ':' << PLoc.getColumn();

Modified: cfe/branches/Apple/whitney/lib/Checker/AnalysisConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Checker/AnalysisConsumer.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Checker/AnalysisConsumer.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Checker/AnalysisConsumer.cpp Mon Nov 15 15:46:47 2010
@@ -143,19 +143,21 @@
 
     SourceManager &SM = Mgr->getASTContext().getSourceManager();
     PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
-    llvm::errs() << "ANALYZE: " << Loc.getFilename();
+    if (Loc.isValid()) {
+      llvm::errs() << "ANALYZE: " << Loc.getFilename();
 
-    if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
-      const NamedDecl *ND = cast<NamedDecl>(D);
-      llvm::errs() << ' ' << ND << '\n';
-    }
-    else if (isa<BlockDecl>(D)) {
-      llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:"
-                   << Loc.getColumn() << '\n';
-    }
-    else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
-      Selector S = MD->getSelector();
-      llvm::errs() << ' ' << S.getAsString();
+      if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
+        const NamedDecl *ND = cast<NamedDecl>(D);
+        llvm::errs() << ' ' << ND << '\n';
+      }
+      else if (isa<BlockDecl>(D)) {
+        llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:"
+                     << Loc.getColumn() << '\n';
+      }
+      else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
+        Selector S = MD->getSelector();
+        llvm::errs() << ' ' << S.getAsString();
+      }
     }
   }
 

Modified: cfe/branches/Apple/whitney/lib/Checker/AnalyzerStatsChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Checker/AnalyzerStatsChecker.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Checker/AnalyzerStatsChecker.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Checker/AnalyzerStatsChecker.cpp Mon Nov 15 15:46:47 2010
@@ -83,16 +83,18 @@
   llvm::SmallString<128> buf;
   llvm::raw_svector_ostream output(buf);
   PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
-  output << Loc.getFilename() << " : ";
+  if (Loc.isValid()) {
+    output << Loc.getFilename() << " : ";
 
-  if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
-    const NamedDecl *ND = cast<NamedDecl>(D);
-    output << ND;
+    if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
+      const NamedDecl *ND = cast<NamedDecl>(D);
+      output << ND;
+    }
+    else if (isa<BlockDecl>(D)) {
+      output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
+    }
   }
-  else if (isa<BlockDecl>(D)) {
-    output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
-  }
-
+  
   output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
       << unreachable << " | Aborted Block: "
       << (Eng.wasBlockAborted() ? "yes" : "no")

Modified: cfe/branches/Apple/whitney/lib/Frontend/DocumentXML.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Frontend/DocumentXML.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Frontend/DocumentXML.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Frontend/DocumentXML.cpp Mon Nov 15 15:46:47 2010
@@ -321,9 +321,11 @@
   if (!SpellingLoc.isInvalid())
   {
     PLoc = SM.getPresumedLoc(SpellingLoc);
-    addSourceFileAttribute(PLoc.getFilename());
-    addAttribute("line", PLoc.getLine());
-    addAttribute("col", PLoc.getColumn());
+    if (PLoc.isValid()) {
+      addSourceFileAttribute(PLoc.getFilename());
+      addAttribute("line", PLoc.getLine());
+      addAttribute("col", PLoc.getColumn());
+    }
   }
   // else there is no error in some cases (eg. CXXThisExpr)
   return PLoc;
@@ -340,8 +342,9 @@
     if (!SpellingLoc.isInvalid())
     {
       PresumedLoc PLoc = SM.getPresumedLoc(SpellingLoc);
-      if (PStartLoc.isInvalid() ||
-          strcmp(PLoc.getFilename(), PStartLoc.getFilename()) != 0) {
+      if (PLoc.isInvalid()) {
+      } else if (PStartLoc.isInvalid() ||
+                 strcmp(PLoc.getFilename(), PStartLoc.getFilename()) != 0) {
         addToMap(SourceFiles, PLoc.getFilename(), ID_FILE);
         addAttribute("endfile", PLoc.getFilename());
         addAttribute("endline", PLoc.getLine());

Modified: cfe/branches/Apple/whitney/lib/Frontend/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Frontend/PrintPreprocessedOutput.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Frontend/PrintPreprocessedOutput.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Frontend/PrintPreprocessedOutput.cpp Mon Nov 15 15:46:47 2010
@@ -131,7 +131,10 @@
 
   bool HandleFirstTokOnLine(Token &Tok);
   bool MoveToLine(SourceLocation Loc) {
-    return MoveToLine(SM.getPresumedLoc(Loc).getLine());
+    PresumedLoc PLoc = SM.getPresumedLoc(Loc);
+    if (PLoc.isInvalid())
+      return false;
+    return MoveToLine(PLoc.getLine());
   }
   bool MoveToLine(unsigned LineNo);
 
@@ -238,10 +241,13 @@
   SourceManager &SourceMgr = SM;
   
   PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
+  if (UserLoc.isInvalid())
+    return;
+  
   unsigned NewLine = UserLoc.getLine();
 
   if (Reason == PPCallbacks::EnterFile) {
-    SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
+    SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
     if (IncludeLoc.isValid())
       MoveToLine(IncludeLoc);
   } else if (Reason == PPCallbacks::SystemHeaderPragma) {
@@ -592,10 +598,18 @@
   // start.
   const SourceManager &SourceMgr = PP.getSourceManager();
   Token Tok;
-  do PP.Lex(Tok);
-  while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
-         !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
-                 "<built-in>"));
+  do {
+    PP.Lex(Tok);
+    if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
+      break;
+
+    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
+    if (PLoc.isInvalid())
+      break;
+
+    if (strcmp(PLoc.getFilename(), "<built-in>"))
+      break;
+  } while (true);
 
   // Read all the preprocessed tokens, printing them out to the stream.
   PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);

Modified: cfe/branches/Apple/whitney/lib/Frontend/TextDiagnosticPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Frontend/TextDiagnosticPrinter.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Frontend/TextDiagnosticPrinter.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Frontend/TextDiagnosticPrinter.cpp Mon Nov 15 15:46:47 2010
@@ -57,7 +57,9 @@
   if (Loc.isInvalid()) return;
 
   PresumedLoc PLoc = SM.getPresumedLoc(Loc);
-
+  if (PLoc.isInvalid())
+    return;
+  
   // Print out the other include frames first.
   PrintIncludeStack(PLoc.getIncludeLoc(), SM);
 
@@ -328,7 +330,9 @@
     if (!Suppressed) {
       // Get the pretty name, according to #line directives etc.
       PresumedLoc PLoc = SM.getPresumedLoc(Loc);
-
+      if (PLoc.isInvalid())
+        return;
+      
       // If this diagnostic is not in the main file, print out the
       // "included from" lines.
       if (LastWarningLoc != PLoc.getIncludeLoc()) {
@@ -567,6 +571,10 @@
 
         // We specifically do not do word-wrapping or tab-expansion here,
         // because this is supposed to be easy to parse.
+        PresumedLoc PLoc = SM.getPresumedLoc(B);
+        if (PLoc.isInvalid())
+          break;
+        
         OS << "fix-it:\"";
         OS.write_escaped(SM.getPresumedLoc(B).getFilename());
         OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second)
@@ -770,6 +778,9 @@
   if (Info.getLocation().isValid()) {
     const SourceManager &SM = Info.getLocation().getManager();
     PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
+    if (PLoc.isInvalid())
+      return;
+    
     unsigned LineNo = PLoc.getLine();
 
     // First, if this diagnostic is not in the main file, print out the

Modified: cfe/branches/Apple/whitney/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Lex/PPDirectives.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Lex/PPDirectives.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Lex/PPDirectives.cpp Mon Nov 15 15:46:47 2010
@@ -804,7 +804,9 @@
     FileID CurFileID =
       SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first;
     PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
-
+    if (PLoc.isInvalid())
+      return true;
+    
     // If there is no include loc (main file) or if the include loc is in a
     // different physical file, then we aren't in a "1" line marker flag region.
     SourceLocation IncLoc = PLoc.getIncludeLoc();

Modified: cfe/branches/Apple/whitney/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Lex/PPMacroExpansion.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Lex/PPMacroExpansion.cpp Mon Nov 15 15:46:47 2010
@@ -713,7 +713,7 @@
     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
 
     // __LINE__ expands to a simple numeric value.
-    OS << PLoc.getLine();
+    OS << (PLoc.isValid()? PLoc.getLine() : 1);
     Tok.setKind(tok::numeric_constant);
   } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
     // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
@@ -722,19 +722,24 @@
 
     // __BASE_FILE__ is a GNU extension that returns the top of the presumed
     // #include stack instead of the current file.
-    if (II == Ident__BASE_FILE__) {
+    if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
       SourceLocation NextLoc = PLoc.getIncludeLoc();
       while (NextLoc.isValid()) {
         PLoc = SourceMgr.getPresumedLoc(NextLoc);
+        if (PLoc.isInvalid())
+          break;
+        
         NextLoc = PLoc.getIncludeLoc();
       }
     }
 
     // Escape this filename.  Turn '\' -> '\\' '"' -> '\"'
     llvm::SmallString<128> FN;
-    FN += PLoc.getFilename();
-    Lexer::Stringify(FN);
-    OS << '"' << FN.str() << '"';
+    if (PLoc.isValid()) {
+      FN += PLoc.getFilename();
+      Lexer::Stringify(FN);
+      OS << '"' << FN.str() << '"';
+    }
     Tok.setKind(tok::string_literal);
   } else if (II == Ident__DATE__) {
     if (!DATELoc.isValid())
@@ -760,9 +765,11 @@
     unsigned Depth = 0;
 
     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
-    PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
-    for (; PLoc.isValid(); ++Depth)
+    if (PLoc.isValid()) {
       PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
+      for (; PLoc.isValid(); ++Depth)
+        PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
+    }
 
     // __INCLUDE_LEVEL__ expands to a simple numeric value.
     OS << Depth;

Modified: cfe/branches/Apple/whitney/lib/Lex/Pragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Lex/Pragma.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Lex/Pragma.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Lex/Pragma.cpp Mon Nov 15 15:46:47 2010
@@ -329,6 +329,9 @@
 
 
   PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
+  if (PLoc.isInvalid())
+    return;
+  
   unsigned FilenameLen = strlen(PLoc.getFilename());
   unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
                                                          FilenameLen);

Modified: cfe/branches/Apple/whitney/tools/libclang/CIndexInclusionStack.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/libclang/CIndexInclusionStack.cpp?rev=119261&r1=119260&r2=119261&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/libclang/CIndexInclusionStack.cpp (original)
+++ cfe/branches/Apple/whitney/tools/libclang/CIndexInclusionStack.cpp Mon Nov 15 15:46:47 2010
@@ -55,7 +55,7 @@
     while (L.isValid()) {
       PresumedLoc PLoc = SM.getPresumedLoc(L);
       InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
-      L = PLoc.getIncludeLoc();
+      L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation();
     }
             
     // Callback to the client.





More information about the llvm-branch-commits mailing list