diff options
author | Chris Lattner <sabre@nondot.org> | 2009-06-15 01:25:23 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-06-15 01:25:23 +0000 |
commit | 3ee211fd15dfeeb51bb69681084cdfcea427f314 (patch) | |
tree | 43dd9f86553962f798e64abe1e93190f8dac7950 /lib/Frontend | |
parent | 2a8e4e1d8548dc3e30d7c9ba92127c7884a11448 (diff) |
Fix PR2741 by making our newline tracking be aware of newlines that
can occur in the middle of comment tokens.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73365 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Frontend')
-rw-r--r-- | lib/Frontend/PrintPreprocessedOutput.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp index 89d099caf8..eb68e3875a 100644 --- a/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/lib/Frontend/PrintPreprocessedOutput.cpp @@ -123,6 +123,8 @@ public: } void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0); + void HandleNewlinesInToken(const char *TokStr, unsigned Len); + /// MacroDefined - This hook is called whenever a macro definition is seen. void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI); @@ -327,6 +329,29 @@ bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) { return true; } +void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr, + unsigned Len) { + unsigned NumNewlines = 0; + for (; Len; --Len, ++TokStr) { + if (*TokStr != '\n' && + *TokStr != '\r') + continue; + + ++NumNewlines; + + // If we have \n\r or \r\n, skip both and count as one line. + if (Len != 1 && + (TokStr[1] == '\n' || TokStr[1] == '\r') && + TokStr[0] != TokStr[1]) + ++TokStr, --Len; + } + + if (NumNewlines == 0) return; + +// CurLine += NumNewlines; +} + + namespace { struct UnknownPragmaHandler : public PragmaHandler { const char *Prefix; @@ -382,9 +407,19 @@ static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, const char *TokPtr = Buffer; unsigned Len = PP.getSpelling(Tok, TokPtr); OS.write(TokPtr, Len); + + // Tokens that can contain embedded newlines need to adjust our current + // line number. + if (Tok.getKind() == tok::comment) + Callbacks->HandleNewlinesInToken(TokPtr, Len); } else { std::string S = PP.getSpelling(Tok); OS.write(&S[0], S.size()); + + // Tokens that can contain embedded newlines need to adjust our current + // line number. + if (Tok.getKind() == tok::comment) + Callbacks->HandleNewlinesInToken(&S[0], S.size()); } Callbacks->SetEmittedTokensOnThisLine(); |