aboutsummaryrefslogtreecommitdiff
path: root/lib/Frontend/PrintPreprocessedOutput.cpp
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2010-11-12 07:15:47 +0000
committerDouglas Gregor <dgregor@apple.com>2010-11-12 07:15:47 +0000
commitcb7b1e17b63967317ab5cc55682168cf0380519a (patch)
treeae64d4fe18e56974c3aae2c99e86aa954410ccdb /lib/Frontend/PrintPreprocessedOutput.cpp
parent8f0889ce457db51d3af1eb1245bceee272d4dc7d (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/Frontend/PrintPreprocessedOutput.cpp')
-rw-r--r--lib/Frontend/PrintPreprocessedOutput.cpp26
1 files changed, 20 insertions, 6 deletions
diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp
index 6019b30833..429999cd5e 100644
--- a/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -131,7 +131,10 @@ public:
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 @@ void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
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) {
@@ -593,10 +599,18 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
// 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);