diff options
author | Chris Lattner <sabre@nondot.org> | 2009-12-01 22:52:33 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-12-01 22:52:33 +0000 |
commit | ed3b360290c4652c8e96bc6869354c73975afe05 (patch) | |
tree | 36f2fb40876c17613aee57705d28cc992ec13ced | |
parent | 0e3b78a6c21b27eb1d73351a6d856778b57de54b (diff) | |
download | bcm5719-llvm-ed3b360290c4652c8e96bc6869354c73975afe05.tar.gz bcm5719-llvm-ed3b360290c4652c8e96bc6869354c73975afe05.zip |
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"
^
llvm-svn: 90276
-rw-r--r-- | clang/include/clang/Basic/DiagnosticLexKinds.td | 2 | ||||
-rw-r--r-- | clang/include/clang/Basic/SourceManager.h | 14 | ||||
-rw-r--r-- | clang/include/clang/Lex/Preprocessor.h | 5 | ||||
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 4 | ||||
-rw-r--r-- | clang/lib/Lex/PPDirectives.cpp | 5 | ||||
-rw-r--r-- | clang/lib/Lex/PPLexerChange.cpp | 6 |
6 files changed, 22 insertions, 14 deletions
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td index 39123d9b371..cc06ab3a869 100644 --- a/clang/include/clang/Basic/DiagnosticLexKinds.td +++ b/clang/include/clang/Basic/DiagnosticLexKinds.td @@ -171,7 +171,7 @@ def err_pp_invalid_directive : Error<"invalid preprocessing directive">; 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>">; diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index 7e9ac531017..67b3d500643 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -77,8 +77,10 @@ namespace SrcMgr { /// 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 @@ public: // 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. diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index edd34b71896..c8e4a0f2097 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -330,8 +330,9 @@ public: /// 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 diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index a85bef0f29e..394f493fa90 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -47,12 +47,12 @@ unsigned ContentCache::getSize() const { 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); diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 9caca339be3..028593f39c7 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -1112,9 +1112,10 @@ void Preprocessor::HandleIncludeDirective(Token &IncludeTok, } // 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. diff --git a/clang/lib/Lex/PPLexerChange.cpp b/clang/lib/Lex/PPLexerChange.cpp index 8a61d7b9c24..1580b87dac6 100644 --- a/clang/lib/Lex/PPLexerChange.cpp +++ b/clang/lib/Lex/PPLexerChange.cpp @@ -64,7 +64,8 @@ PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { /// 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 @@ bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir) { } // 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; |