diff options
author | Douglas Gregor <dgregor@apple.com> | 2010-11-12 07:15:47 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2010-11-12 07:15:47 +0000 |
commit | cb7b1e17b63967317ab5cc55682168cf0380519a (patch) | |
tree | ae64d4fe18e56974c3aae2c99e86aa954410ccdb /lib/Lex/PPMacroExpansion.cpp | |
parent | 8f0889ce457db51d3af1eb1245bceee272d4dc7d (diff) |
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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@118885 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/PPMacroExpansion.cpp')
-rw-r--r-- | lib/Lex/PPMacroExpansion.cpp | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp index b779dd5654..2428f9af45 100644 --- a/lib/Lex/PPMacroExpansion.cpp +++ b/lib/Lex/PPMacroExpansion.cpp @@ -723,7 +723,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { 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 @@ -732,19 +732,24 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { // __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()) @@ -770,9 +775,11 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { 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; |