[cfe-commits] r90276 - in /cfe/trunk: include/clang/Basic/DiagnosticLexKinds.td include/clang/Basic/SourceManager.h include/clang/Lex/Preprocessor.h lib/Basic/SourceManager.cpp lib/Lex/PPDirectives.cpp lib/Lex/PPLexerChange.cpp

Chris Lattner sabre at nondot.org
Tue Dec 1 14:52:33 PST 2009


Author: lattner
Date: Tue Dec  1 16:52:33 2009
New Revision: 90276

URL: http://llvm.org/viewvc/llvm-project?rev=90276&view=rev
Log:
pass the reason for failure up from MemoryBuffer and report it
in diagnostics when we fail to open a file.  This allows us to
report things like:

$ clang test.c -I.
test.c:2:10: fatal error: error opening file './foo.h': Permission denied
#include "foo.h"
         ^


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
    cfe/trunk/include/clang/Basic/SourceManager.h
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Basic/SourceManager.cpp
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/lib/Lex/PPLexerChange.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td?rev=90276&r1=90275&r2=90276&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td Tue Dec  1 16:52:33 2009
@@ -171,7 +171,7 @@
 def err_pp_hash_error : Error<"#error%0">;
 def err_pp_file_not_found : Error<"'%0' file not found">, DefaultFatal;
 def err_pp_error_opening_file : Error<
-  "error opening file '%0'">, DefaultFatal;
+  "error opening file '%0': %1">, DefaultFatal;
 def err_pp_empty_filename : Error<"empty filename">;
 def err_pp_include_too_deep : Error<"#include nested too deeply">;
 def err_pp_expects_filename : Error<"expected \"FILENAME\" or <FILENAME>">;

Modified: cfe/trunk/include/clang/Basic/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/SourceManager.h?rev=90276&r1=90275&r2=90276&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Tue Dec  1 16:52:33 2009
@@ -77,8 +77,10 @@
     /// ContentCache.
     mutable FileID FirstFID;
 
-    /// getBuffer - Returns the memory buffer for the associated content.
-    const llvm::MemoryBuffer *getBuffer() const;
+    /// getBuffer - Returns the memory buffer for the associated content.  If
+    /// there is an error opening this buffer the first time, this returns null
+    /// and fills in the ErrorStr with a reason.
+    const llvm::MemoryBuffer *getBuffer(std::string *ErrorStr = 0) const;
 
     /// getSize - Returns the size of the content encapsulated by this
     ///  ContentCache. This can be the size of the source file or the size of an
@@ -432,10 +434,12 @@
   // FileID manipulation methods.
   //===--------------------------------------------------------------------===//
 
-  /// getBuffer - Return the buffer for the specified FileID.
+  /// getBuffer - Return the buffer for the specified FileID.  If there is an
+  /// error opening this buffer the first time, this returns null and fills in
+  /// the ErrorStr with a reason.
   ///
-  const llvm::MemoryBuffer *getBuffer(FileID FID) const {
-    return getSLocEntry(FID).getFile().getContentCache()->getBuffer();
+  const llvm::MemoryBuffer *getBuffer(FileID FID, std::string *Error = 0) const{
+    return getSLocEntry(FID).getFile().getContentCache()->getBuffer(Error);
   }
 
   /// getFileEntryForID - Returns the FileEntry record for the provided FileID.

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=90276&r1=90275&r2=90276&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Tue Dec  1 16:52:33 2009
@@ -330,8 +330,9 @@
 
   /// EnterSourceFile - Add a source file to the top of the include stack and
   /// start lexing tokens from it instead of the current buffer.  Return true
-  /// on failure.
-  bool EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir);
+  /// and fill in ErrorStr with the error information on failure.
+  bool EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir,
+                       std::string *ErrorStr = 0);
 
   /// EnterMacro - Add a Macro to the top of the include stack and start lexing
   /// tokens from it instead of the current buffer.  Args specifies the

Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=90276&r1=90275&r2=90276&view=diff

==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Tue Dec  1 16:52:33 2009
@@ -47,12 +47,12 @@
   return Buffer ? Buffer->getBufferSize() : Entry->getSize();
 }
 
-const llvm::MemoryBuffer *ContentCache::getBuffer() const {
+const llvm::MemoryBuffer *ContentCache::getBuffer(std::string *ErrorStr) const {
   // Lazily create the Buffer for ContentCaches that wrap files.
   if (!Buffer && Entry) {
     // FIXME: Should we support a way to not have to do this check over
     //   and over if we cannot open the file?
-    Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
+    Buffer = MemoryBuffer::getFile(Entry->getName(), ErrorStr,Entry->getSize());
     if (isTruncated())
       const_cast<ContentCache *>(this)->truncateAt(TruncateAtLine, 
                                                    TruncateAtColumn);

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=90276&r1=90275&r2=90276&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Tue Dec  1 16:52:33 2009
@@ -1112,9 +1112,10 @@
   }
 
   // Finally, if all is good, enter the new file!
-  if (EnterSourceFile(FID, CurDir))
+  std::string ErrorStr;
+  if (EnterSourceFile(FID, CurDir, &ErrorStr))
     Diag(FilenameTok, diag::err_pp_error_opening_file)
-      << std::string(SourceMgr.getFileEntryForID(FID)->getName());
+      << std::string(SourceMgr.getFileEntryForID(FID)->getName()) << ErrorStr;
 }
 
 /// HandleIncludeNextDirective - Implements #include_next.

Modified: cfe/trunk/lib/Lex/PPLexerChange.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPLexerChange.cpp?rev=90276&r1=90275&r2=90276&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PPLexerChange.cpp (original)
+++ cfe/trunk/lib/Lex/PPLexerChange.cpp Tue Dec  1 16:52:33 2009
@@ -64,7 +64,8 @@
 
 /// EnterSourceFile - Add a source file to the top of the include stack and
 /// start lexing tokens from it instead of the current buffer.
-bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir) {
+bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir,
+                                   std::string *ErrorStr) {
   assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!");
   ++NumEnteredSourceFiles;
 
@@ -79,7 +80,8 @@
   }
   
   // Get the MemoryBuffer for this FID, if it fails, we fail.
-  const llvm::MemoryBuffer *InputFile = getSourceManager().getBuffer(FID);
+  const llvm::MemoryBuffer *InputFile =
+    getSourceManager().getBuffer(FID, ErrorStr);
   if (InputFile == 0)
     return true;
   





More information about the cfe-commits mailing list