Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This code simply runs the preprocessor on the input file and prints out the |
| 11 | // result. This is the traditional behavior of the -E option. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Eli Friedman | b09f6e1 | 2009-05-19 04:14:29 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/Utils.h" |
Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Frontend/PreprocessorOutputOptions.h" |
Chris Lattner | f73903a | 2009-02-06 06:45:26 +0000 | [diff] [blame] | 19 | #include "clang/Lex/MacroInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Lex/PPCallbacks.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Pragma.h" |
Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 23 | #include "clang/Lex/TokenConcatenation.h" |
Benjamin Kramer | 3ff2a4b | 2010-01-19 17:42:20 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringRef.h" |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Joerg Sonnenberger | 7094dee | 2012-08-10 10:58:18 +0000 | [diff] [blame] | 29 | #include <cctype> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | #include <cstdio> |
| 31 | using namespace clang; |
| 32 | |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 33 | /// PrintMacroDefinition - Print a macro definition in a form that will be |
| 34 | /// properly accepted back as a definition. |
| 35 | static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 36 | Preprocessor &PP, raw_ostream &OS) { |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 37 | OS << "#define " << II.getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 | |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 39 | if (MI.isFunctionLike()) { |
| 40 | OS << '('; |
Chris Lattner | 13d5558 | 2009-12-09 02:08:14 +0000 | [diff] [blame] | 41 | if (!MI.arg_empty()) { |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 42 | MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end(); |
Chris Lattner | 13d5558 | 2009-12-09 02:08:14 +0000 | [diff] [blame] | 43 | for (; AI+1 != E; ++AI) { |
| 44 | OS << (*AI)->getName(); |
Chris Lattner | 807b93e | 2009-12-07 01:58:34 +0000 | [diff] [blame] | 45 | OS << ','; |
Chris Lattner | 807b93e | 2009-12-07 01:58:34 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 13d5558 | 2009-12-09 02:08:14 +0000 | [diff] [blame] | 47 | |
| 48 | // Last argument. |
| 49 | if ((*AI)->getName() == "__VA_ARGS__") |
| 50 | OS << "..."; |
| 51 | else |
| 52 | OS << (*AI)->getName(); |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 53 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 807b93e | 2009-12-07 01:58:34 +0000 | [diff] [blame] | 55 | if (MI.isGNUVarargs()) |
| 56 | OS << "..."; // #define foo(x...) |
Kovarththanan Rajaratnam | a5d10c4 | 2010-03-07 07:30:06 +0000 | [diff] [blame] | 57 | |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 58 | OS << ')'; |
| 59 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 61 | // GCC always emits a space, even if the macro body is empty. However, do not |
| 62 | // want to emit two spaces if the first token has a leading space. |
| 63 | if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace()) |
| 64 | OS << ' '; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 66 | SmallString<128> SpellingBuffer; |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 67 | for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end(); |
| 68 | I != E; ++I) { |
| 69 | if (I->hasLeadingSpace()) |
| 70 | OS << ' '; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 72 | OS << PP.getSpelling(*I, SpellingBuffer); |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | //===----------------------------------------------------------------------===// |
| 77 | // Preprocessed token printer |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | namespace { |
| 81 | class PrintPPOutputPPCallbacks : public PPCallbacks { |
| 82 | Preprocessor &PP; |
Chris Lattner | df72140 | 2010-04-13 00:06:42 +0000 | [diff] [blame] | 83 | SourceManager &SM; |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 84 | TokenConcatenation ConcatInfo; |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 85 | public: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 86 | raw_ostream &OS; |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 87 | private: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | unsigned CurLine; |
Daniel Dunbar | f7c16d9 | 2010-08-24 22:44:13 +0000 | [diff] [blame] | 89 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | bool EmittedTokensOnThisLine; |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 91 | bool EmittedDirectiveOnThisLine; |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 92 | SrcMgr::CharacteristicKind FileType; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 93 | SmallString<512> CurFilename; |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 94 | bool Initialized; |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 95 | bool DisableLineMarkers; |
| 96 | bool DumpDefines; |
Chris Lattner | f744934 | 2009-12-07 01:42:56 +0000 | [diff] [blame] | 97 | bool UseLineDirective; |
Daniel Dunbar | 2a9be3e | 2012-11-16 01:51:11 +0000 | [diff] [blame] | 98 | bool IsFirstFileEntered; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | public: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 100 | PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 101 | bool lineMarkers, bool defines) |
Chris Lattner | df72140 | 2010-04-13 00:06:42 +0000 | [diff] [blame] | 102 | : PP(pp), SM(PP.getSourceManager()), |
| 103 | ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers), |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 104 | DumpDefines(defines) { |
| 105 | CurLine = 0; |
Chris Lattner | d8e3083 | 2007-07-24 06:57:14 +0000 | [diff] [blame] | 106 | CurFilename += "<uninit>"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | EmittedTokensOnThisLine = false; |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 108 | EmittedDirectiveOnThisLine = false; |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 109 | FileType = SrcMgr::C_User; |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 110 | Initialized = false; |
Daniel Dunbar | 2a9be3e | 2012-11-16 01:51:11 +0000 | [diff] [blame] | 111 | IsFirstFileEntered = false; |
Kovarththanan Rajaratnam | a5d10c4 | 2010-03-07 07:30:06 +0000 | [diff] [blame] | 112 | |
Chris Lattner | f744934 | 2009-12-07 01:42:56 +0000 | [diff] [blame] | 113 | // If we're in microsoft mode, use normal #line instead of line markers. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 114 | UseLineDirective = PP.getLangOpts().MicrosoftExt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 117 | void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } |
Chris Lattner | f0f2b29 | 2007-07-23 06:09:34 +0000 | [diff] [blame] | 118 | bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 120 | void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; } |
| 121 | bool hasEmittedDirectiveOnThisLine() const { |
| 122 | return EmittedDirectiveOnThisLine; |
| 123 | } |
| 124 | |
| 125 | bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true); |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 126 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 128 | SrcMgr::CharacteristicKind FileType, |
| 129 | FileID PrevFID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | virtual void Ident(SourceLocation Loc, const std::string &str); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 132 | const std::string &Str); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 133 | virtual void PragmaMessage(SourceLocation Loc, StringRef Str); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 134 | virtual void PragmaDiagnosticPush(SourceLocation Loc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 135 | StringRef Namespace); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 136 | virtual void PragmaDiagnosticPop(SourceLocation Loc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 137 | StringRef Namespace); |
| 138 | virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, |
Benjamin Kramer | d7a3e2c | 2012-02-07 22:29:24 +0000 | [diff] [blame] | 139 | diag::Mapping Map, StringRef Str); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 5f18032 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 141 | bool HandleFirstTokOnLine(Token &Tok); |
Hal Finkel | 43bb45d | 2013-01-28 04:37:37 +0000 | [diff] [blame^] | 142 | |
| 143 | /// Move to the line of the provided source location. This will |
| 144 | /// return true if the output stream required adjustment or if |
| 145 | /// the requested location is on the first line. |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 146 | bool MoveToLine(SourceLocation Loc) { |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 147 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 148 | if (PLoc.isInvalid()) |
| 149 | return false; |
Hal Finkel | 43bb45d | 2013-01-28 04:37:37 +0000 | [diff] [blame^] | 150 | return MoveToLine(PLoc.getLine()) || (PLoc.getLine() == 1); |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 151 | } |
| 152 | bool MoveToLine(unsigned LineNo); |
| 153 | |
Chris Lattner | 8877321 | 2010-04-14 03:57:19 +0000 | [diff] [blame] | 154 | bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok, |
| 155 | const Token &Tok) { |
| 156 | return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok); |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 157 | } |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 158 | void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0); |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 159 | bool LineMarkersAreDisabled() const { return DisableLineMarkers; } |
Chris Lattner | 3ee211f | 2009-06-15 01:25:23 +0000 | [diff] [blame] | 160 | void HandleNewlinesInToken(const char *TokStr, unsigned Len); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 162 | /// MacroDefined - This hook is called whenever a macro definition is seen. |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 163 | void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Benjamin Kramer | 2f05449 | 2010-08-07 22:27:00 +0000 | [diff] [blame] | 165 | /// MacroUndefined - This hook is called whenever a macro #undef is seen. |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 166 | void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | }; |
Chris Lattner | 5db17c9 | 2008-04-08 04:16:20 +0000 | [diff] [blame] | 168 | } // end anonymous namespace |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 170 | void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo, |
| 171 | const char *Extra, |
| 172 | unsigned ExtraLen) { |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 173 | startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false); |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 174 | |
Chris Lattner | f744934 | 2009-12-07 01:42:56 +0000 | [diff] [blame] | 175 | // Emit #line directives or GNU line markers depending on what mode we're in. |
| 176 | if (UseLineDirective) { |
| 177 | OS << "#line" << ' ' << LineNo << ' ' << '"'; |
Ted Kremenek | 2346513 | 2010-09-17 00:41:18 +0000 | [diff] [blame] | 178 | OS.write(CurFilename.data(), CurFilename.size()); |
Chris Lattner | f744934 | 2009-12-07 01:42:56 +0000 | [diff] [blame] | 179 | OS << '"'; |
| 180 | } else { |
| 181 | OS << '#' << ' ' << LineNo << ' ' << '"'; |
Ted Kremenek | 2346513 | 2010-09-17 00:41:18 +0000 | [diff] [blame] | 182 | OS.write(CurFilename.data(), CurFilename.size()); |
Chris Lattner | f744934 | 2009-12-07 01:42:56 +0000 | [diff] [blame] | 183 | OS << '"'; |
Kovarththanan Rajaratnam | a5d10c4 | 2010-03-07 07:30:06 +0000 | [diff] [blame] | 184 | |
Steve Naroff | d76fbda | 2009-12-06 01:02:14 +0000 | [diff] [blame] | 185 | if (ExtraLen) |
| 186 | OS.write(Extra, ExtraLen); |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 187 | |
Steve Naroff | d76fbda | 2009-12-06 01:02:14 +0000 | [diff] [blame] | 188 | if (FileType == SrcMgr::C_System) |
| 189 | OS.write(" 3", 2); |
| 190 | else if (FileType == SrcMgr::C_ExternCSystem) |
| 191 | OS.write(" 3 4", 4); |
| 192 | } |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 193 | OS << '\n'; |
| 194 | } |
| 195 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | /// MoveToLine - Move the output to the source line specified by the location |
| 197 | /// object. We can do this by emitting some number of \n's, or be emitting a |
Chris Lattner | 5f18032 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 198 | /// #line directive. This returns false if already at the specified line, true |
| 199 | /// if some newlines were emitted. |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 200 | bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | // If this line is "close enough" to the original line, just print newlines, |
| 202 | // otherwise print a #line directive. |
Daniel Dunbar | fd96684 | 2008-09-26 01:13:35 +0000 | [diff] [blame] | 203 | if (LineNo-CurLine <= 8) { |
Chris Lattner | 822f940 | 2007-07-23 05:14:05 +0000 | [diff] [blame] | 204 | if (LineNo-CurLine == 1) |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 205 | OS << '\n'; |
Chris Lattner | 5f18032 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 206 | else if (LineNo == CurLine) |
Chandler Carruth | 9f79a1f | 2011-07-14 16:14:52 +0000 | [diff] [blame] | 207 | return false; // Spelling line moved, but expansion line didn't. |
Chris Lattner | 822f940 | 2007-07-23 05:14:05 +0000 | [diff] [blame] | 208 | else { |
| 209 | const char *NewLines = "\n\n\n\n\n\n\n\n"; |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 210 | OS.write(NewLines, LineNo-CurLine); |
Chris Lattner | 822f940 | 2007-07-23 05:14:05 +0000 | [diff] [blame] | 211 | } |
Chris Lattner | 6133aeb | 2010-06-12 16:20:56 +0000 | [diff] [blame] | 212 | } else if (!DisableLineMarkers) { |
| 213 | // Emit a #line or line marker. |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 214 | WriteLineInfo(LineNo, 0, 0); |
Chris Lattner | 6133aeb | 2010-06-12 16:20:56 +0000 | [diff] [blame] | 215 | } else { |
| 216 | // Okay, we're in -P mode, which turns off line markers. However, we still |
| 217 | // need to emit a newline between tokens on different lines. |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 218 | startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | } |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 220 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | CurLine = LineNo; |
Chris Lattner | 5f18032 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 222 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 225 | bool |
| 226 | PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) { |
| 227 | if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) { |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 228 | OS << '\n'; |
| 229 | EmittedTokensOnThisLine = false; |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 230 | EmittedDirectiveOnThisLine = false; |
| 231 | if (ShouldUpdateCurrentLine) |
| 232 | ++CurLine; |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
| 236 | return false; |
| 237 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | |
| 239 | /// FileChanged - Whenever the preprocessor enters or exits a #include file |
| 240 | /// it invokes this handler. Update our conception of the current source |
| 241 | /// position. |
| 242 | void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, |
| 243 | FileChangeReason Reason, |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 244 | SrcMgr::CharacteristicKind NewFileType, |
| 245 | FileID PrevFID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 246 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 247 | // #include directive was at. |
Chris Lattner | df72140 | 2010-04-13 00:06:42 +0000 | [diff] [blame] | 248 | SourceManager &SourceMgr = SM; |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 249 | |
| 250 | PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 251 | if (UserLoc.isInvalid()) |
| 252 | return; |
| 253 | |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 254 | unsigned NewLine = UserLoc.getLine(); |
Daniel Dunbar | f7c16d9 | 2010-08-24 22:44:13 +0000 | [diff] [blame] | 255 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | if (Reason == PPCallbacks::EnterFile) { |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 257 | SourceLocation IncludeLoc = UserLoc.getIncludeLoc(); |
Chris Lattner | 71d8bfb | 2009-01-30 18:44:17 +0000 | [diff] [blame] | 258 | if (IncludeLoc.isValid()) |
| 259 | MoveToLine(IncludeLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | } else if (Reason == PPCallbacks::SystemHeaderPragma) { |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 261 | MoveToLine(NewLine); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | // TODO GCC emits the # directive for this directive on the line AFTER the |
| 264 | // directive and emits a bunch of spaces that aren't needed. Emulate this |
| 265 | // strange behavior. |
| 266 | } |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 267 | |
| 268 | CurLine = NewLine; |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 269 | |
Chris Lattner | d8e3083 | 2007-07-24 06:57:14 +0000 | [diff] [blame] | 270 | CurFilename.clear(); |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 271 | CurFilename += UserLoc.getFilename(); |
Chris Lattner | d8e3083 | 2007-07-24 06:57:14 +0000 | [diff] [blame] | 272 | Lexer::Stringify(CurFilename); |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 273 | FileType = NewFileType; |
| 274 | |
Eli Friedman | 086cc83 | 2013-01-09 03:16:42 +0000 | [diff] [blame] | 275 | if (DisableLineMarkers) { |
| 276 | startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false); |
| 277 | return; |
| 278 | } |
Daniel Dunbar | f7c16d9 | 2010-08-24 22:44:13 +0000 | [diff] [blame] | 279 | |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 280 | if (!Initialized) { |
| 281 | WriteLineInfo(CurLine); |
| 282 | Initialized = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | } |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 284 | |
Daniel Dunbar | 2a9be3e | 2012-11-16 01:51:11 +0000 | [diff] [blame] | 285 | // Do not emit an enter marker for the main file (which we expect is the first |
| 286 | // entered file). This matches gcc, and improves compatibility with some tools |
| 287 | // which track the # line markers as a way to determine when the preprocessed |
| 288 | // output is in the context of the main file. |
| 289 | if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) { |
| 290 | IsFirstFileEntered = true; |
| 291 | return; |
| 292 | } |
| 293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | switch (Reason) { |
| 295 | case PPCallbacks::EnterFile: |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 296 | WriteLineInfo(CurLine, " 1", 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | break; |
| 298 | case PPCallbacks::ExitFile: |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 299 | WriteLineInfo(CurLine, " 2", 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | case PPCallbacks::SystemHeaderPragma: |
| 302 | case PPCallbacks::RenameFile: |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 303 | WriteLineInfo(CurLine); |
| 304 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 305 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 308 | /// Ident - Handle #ident directives when read by the preprocessor. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | /// |
| 310 | void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) { |
| 311 | MoveToLine(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 313 | OS.write("#ident ", strlen("#ident ")); |
| 314 | OS.write(&S[0], S.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | EmittedTokensOnThisLine = true; |
| 316 | } |
| 317 | |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 318 | /// MacroDefined - This hook is called whenever a macro definition is seen. |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 319 | void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 320 | const MacroInfo *MI) { |
| 321 | // Only print out macro definitions in -dD mode. |
| 322 | if (!DumpDefines || |
| 323 | // Ignore __FILE__ etc. |
| 324 | MI->isBuiltinMacro()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 326 | MoveToLine(MI->getDefinitionLoc()); |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 327 | PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS); |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 328 | setEmittedDirectiveOnThisLine(); |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 331 | void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, |
Benjamin Kramer | 2f05449 | 2010-08-07 22:27:00 +0000 | [diff] [blame] | 332 | const MacroInfo *MI) { |
| 333 | // Only print out macro definitions in -dD mode. |
| 334 | if (!DumpDefines) return; |
| 335 | |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 336 | MoveToLine(MacroNameTok.getLocation()); |
| 337 | OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName(); |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 338 | setEmittedDirectiveOnThisLine(); |
Benjamin Kramer | 2f05449 | 2010-08-07 22:27:00 +0000 | [diff] [blame] | 339 | } |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 340 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 341 | void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | const IdentifierInfo *Kind, |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 343 | const std::string &Str) { |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 344 | startNewLineIfNeeded(); |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 345 | MoveToLine(Loc); |
| 346 | OS << "#pragma comment(" << Kind->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 348 | if (!Str.empty()) { |
| 349 | OS << ", \""; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 351 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 352 | unsigned char Char = Str[i]; |
Chris Lattner | 52a3e9e | 2009-01-16 22:13:37 +0000 | [diff] [blame] | 353 | if (isprint(Char) && Char != '\\' && Char != '"') |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 354 | OS << (char)Char; |
| 355 | else // Output anything hard as an octal escape. |
| 356 | OS << '\\' |
| 357 | << (char)('0'+ ((Char >> 6) & 7)) |
| 358 | << (char)('0'+ ((Char >> 3) & 7)) |
| 359 | << (char)('0'+ ((Char >> 0) & 7)); |
| 360 | } |
| 361 | OS << '"'; |
| 362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 364 | OS << ')'; |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 365 | setEmittedDirectiveOnThisLine(); |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 368 | void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 369 | StringRef Str) { |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 370 | startNewLineIfNeeded(); |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 371 | MoveToLine(Loc); |
| 372 | OS << "#pragma message("; |
| 373 | |
| 374 | OS << '"'; |
| 375 | |
| 376 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 377 | unsigned char Char = Str[i]; |
| 378 | if (isprint(Char) && Char != '\\' && Char != '"') |
| 379 | OS << (char)Char; |
| 380 | else // Output anything hard as an octal escape. |
| 381 | OS << '\\' |
| 382 | << (char)('0'+ ((Char >> 6) & 7)) |
| 383 | << (char)('0'+ ((Char >> 3) & 7)) |
| 384 | << (char)('0'+ ((Char >> 0) & 7)); |
| 385 | } |
| 386 | OS << '"'; |
| 387 | |
| 388 | OS << ')'; |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 389 | setEmittedDirectiveOnThisLine(); |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 392 | void PrintPPOutputPPCallbacks:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 393 | PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) { |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 394 | startNewLineIfNeeded(); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 395 | MoveToLine(Loc); |
| 396 | OS << "#pragma " << Namespace << " diagnostic push"; |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 397 | setEmittedDirectiveOnThisLine(); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void PrintPPOutputPPCallbacks:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 401 | PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) { |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 402 | startNewLineIfNeeded(); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 403 | MoveToLine(Loc); |
| 404 | OS << "#pragma " << Namespace << " diagnostic pop"; |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 405 | setEmittedDirectiveOnThisLine(); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | void PrintPPOutputPPCallbacks:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 409 | PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, |
Benjamin Kramer | d7a3e2c | 2012-02-07 22:29:24 +0000 | [diff] [blame] | 410 | diag::Mapping Map, StringRef Str) { |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 411 | startNewLineIfNeeded(); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 412 | MoveToLine(Loc); |
| 413 | OS << "#pragma " << Namespace << " diagnostic "; |
Benjamin Kramer | d7a3e2c | 2012-02-07 22:29:24 +0000 | [diff] [blame] | 414 | switch (Map) { |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 415 | case diag::MAP_WARNING: |
| 416 | OS << "warning"; |
| 417 | break; |
| 418 | case diag::MAP_ERROR: |
| 419 | OS << "error"; |
| 420 | break; |
| 421 | case diag::MAP_IGNORE: |
| 422 | OS << "ignored"; |
| 423 | break; |
| 424 | case diag::MAP_FATAL: |
| 425 | OS << "fatal"; |
| 426 | break; |
| 427 | } |
| 428 | OS << " \"" << Str << '"'; |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 429 | setEmittedDirectiveOnThisLine(); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 430 | } |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 431 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 432 | /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this |
Chris Lattner | 5f18032 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 433 | /// is called for the first token on each new line. If this really is the start |
| 434 | /// of a new logical line, handle it and return true, otherwise return false. |
| 435 | /// This may not be the start of a logical line because the "start of line" |
Chandler Carruth | 9f79a1f | 2011-07-14 16:14:52 +0000 | [diff] [blame] | 436 | /// marker is set for spelling lines, not expansion ones. |
Chris Lattner | 5f18032 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 437 | bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 438 | // Figure out what line we went to and insert the appropriate number of |
| 439 | // newline characters. |
Chris Lattner | 5f18032 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 440 | if (!MoveToLine(Tok.getLocation())) |
| 441 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | // Print out space characters so that the first token on a line is |
| 444 | // indented for easy reading. |
Chandler Carruth | a77c031 | 2011-07-25 20:57:57 +0000 | [diff] [blame] | 445 | unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 447 | // This hack prevents stuff like: |
| 448 | // #define HASH # |
| 449 | // HASH define foo bar |
| 450 | // From having the # character end up at column 1, which makes it so it |
| 451 | // is not handled as a #define next time through the preprocessor if in |
| 452 | // -fpreprocessed mode. |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 453 | if (ColNo <= 1 && Tok.is(tok::hash)) |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 454 | OS << ' '; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 456 | // Otherwise, indent the appropriate number of spaces. |
| 457 | for (; ColNo > 1; --ColNo) |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 458 | OS << ' '; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 5f18032 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 460 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Chris Lattner | 3ee211f | 2009-06-15 01:25:23 +0000 | [diff] [blame] | 463 | void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr, |
| 464 | unsigned Len) { |
| 465 | unsigned NumNewlines = 0; |
| 466 | for (; Len; --Len, ++TokStr) { |
| 467 | if (*TokStr != '\n' && |
| 468 | *TokStr != '\r') |
| 469 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 3ee211f | 2009-06-15 01:25:23 +0000 | [diff] [blame] | 471 | ++NumNewlines; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 3ee211f | 2009-06-15 01:25:23 +0000 | [diff] [blame] | 473 | // If we have \n\r or \r\n, skip both and count as one line. |
| 474 | if (Len != 1 && |
| 475 | (TokStr[1] == '\n' || TokStr[1] == '\r') && |
| 476 | TokStr[0] != TokStr[1]) |
| 477 | ++TokStr, --Len; |
| 478 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 3ee211f | 2009-06-15 01:25:23 +0000 | [diff] [blame] | 480 | if (NumNewlines == 0) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Chris Lattner | ed2d7c4 | 2009-06-15 04:08:28 +0000 | [diff] [blame] | 482 | CurLine += NumNewlines; |
Chris Lattner | 3ee211f | 2009-06-15 01:25:23 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 486 | namespace { |
| 487 | struct UnknownPragmaHandler : public PragmaHandler { |
| 488 | const char *Prefix; |
| 489 | PrintPPOutputPPCallbacks *Callbacks; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 491 | UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks) |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 492 | : Prefix(prefix), Callbacks(callbacks) {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 493 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 494 | Token &PragmaTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | // Figure out what line we went to and insert the appropriate number of |
| 496 | // newline characters. |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 497 | Callbacks->startNewLineIfNeeded(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 498 | Callbacks->MoveToLine(PragmaTok.getLocation()); |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 499 | Callbacks->OS.write(Prefix, strlen(Prefix)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | // Read and print all of the pragma tokens. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 501 | while (PragmaTok.isNot(tok::eod)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 502 | if (PragmaTok.hasLeadingSpace()) |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 503 | Callbacks->OS << ' '; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 504 | std::string TokSpell = PP.getSpelling(PragmaTok); |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 505 | Callbacks->OS.write(&TokSpell[0], TokSpell.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 506 | PP.LexUnexpandedToken(PragmaTok); |
| 507 | } |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 508 | Callbacks->setEmittedDirectiveOnThisLine(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 509 | } |
| 510 | }; |
| 511 | } // end anonymous namespace |
| 512 | |
Chris Lattner | f0f2b29 | 2007-07-23 06:09:34 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 59076ab | 2009-02-06 05:56:11 +0000 | [diff] [blame] | 514 | static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, |
| 515 | PrintPPOutputPPCallbacks *Callbacks, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 516 | raw_ostream &OS) { |
Benjamin Kramer | e396007 | 2010-02-27 18:02:51 +0000 | [diff] [blame] | 517 | char Buffer[256]; |
Chris Lattner | c54539c | 2010-06-15 21:06:38 +0000 | [diff] [blame] | 518 | Token PrevPrevTok, PrevTok; |
| 519 | PrevPrevTok.startToken(); |
| 520 | PrevTok.startToken(); |
Chris Lattner | 6f688e1 | 2007-10-10 20:45:16 +0000 | [diff] [blame] | 521 | while (1) { |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 522 | if (Callbacks->hasEmittedDirectiveOnThisLine()) { |
| 523 | Callbacks->startNewLineIfNeeded(); |
| 524 | Callbacks->MoveToLine(Tok.getLocation()); |
| 525 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 527 | // If this token is at the start of a line, emit newlines if needed. |
Chris Lattner | 5f18032 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 528 | if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) { |
| 529 | // done. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | } else if (Tok.hasLeadingSpace() || |
Chris Lattner | f0f2b29 | 2007-07-23 06:09:34 +0000 | [diff] [blame] | 531 | // If we haven't emitted a token on this line yet, PrevTok isn't |
| 532 | // useful to look at and no concatenation could happen anyway. |
Chris Lattner | b638a30 | 2007-07-23 23:21:34 +0000 | [diff] [blame] | 533 | (Callbacks->hasEmittedTokensOnThisLine() && |
Chris Lattner | f0f2b29 | 2007-07-23 06:09:34 +0000 | [diff] [blame] | 534 | // Don't print "-" next to "-", it would form "--". |
Chris Lattner | 8877321 | 2010-04-14 03:57:19 +0000 | [diff] [blame] | 535 | Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) { |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 536 | OS << ' '; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 537 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
Benjamin Kramer | e396007 | 2010-02-27 18:02:51 +0000 | [diff] [blame] | 539 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 540 | OS << II->getName(); |
| 541 | } else if (Tok.isLiteral() && !Tok.needsCleaning() && |
| 542 | Tok.getLiteralData()) { |
| 543 | OS.write(Tok.getLiteralData(), Tok.getLength()); |
| 544 | } else if (Tok.getLength() < 256) { |
| 545 | const char *TokPtr = Buffer; |
| 546 | unsigned Len = PP.getSpelling(Tok, TokPtr); |
| 547 | OS.write(TokPtr, Len); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Benjamin Kramer | e396007 | 2010-02-27 18:02:51 +0000 | [diff] [blame] | 549 | // Tokens that can contain embedded newlines need to adjust our current |
| 550 | // line number. |
| 551 | if (Tok.getKind() == tok::comment) |
| 552 | Callbacks->HandleNewlinesInToken(TokPtr, Len); |
| 553 | } else { |
| 554 | std::string S = PP.getSpelling(Tok); |
| 555 | OS.write(&S[0], S.size()); |
| 556 | |
| 557 | // Tokens that can contain embedded newlines need to adjust our current |
| 558 | // line number. |
| 559 | if (Tok.getKind() == tok::comment) |
| 560 | Callbacks->HandleNewlinesInToken(&S[0], S.size()); |
| 561 | } |
Jordan Rose | 0cdd1fe | 2012-06-15 23:33:51 +0000 | [diff] [blame] | 562 | Callbacks->setEmittedTokensOnThisLine(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | |
Chris Lattner | 6f688e1 | 2007-10-10 20:45:16 +0000 | [diff] [blame] | 564 | if (Tok.is(tok::eof)) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 8877321 | 2010-04-14 03:57:19 +0000 | [diff] [blame] | 566 | PrevPrevTok = PrevTok; |
Chris Lattner | 6f688e1 | 2007-10-10 20:45:16 +0000 | [diff] [blame] | 567 | PrevTok = Tok; |
| 568 | PP.Lex(Tok); |
| 569 | } |
Chris Lattner | 59076ab | 2009-02-06 05:56:11 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Dmitri Gribenko | b395847 | 2013-01-14 00:36:42 +0000 | [diff] [blame] | 572 | typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair; |
Benjamin Kramer | 3ff2a4b | 2010-01-19 17:42:20 +0000 | [diff] [blame] | 573 | static int MacroIDCompare(const void* a, const void* b) { |
| 574 | const id_macro_pair *LHS = static_cast<const id_macro_pair*>(a); |
| 575 | const id_macro_pair *RHS = static_cast<const id_macro_pair*>(b); |
| 576 | return LHS->first->getName().compare(RHS->first->getName()); |
Chris Lattner | 2a2bb18 | 2009-02-10 22:28:19 +0000 | [diff] [blame] | 577 | } |
Chris Lattner | 59076ab | 2009-02-06 05:56:11 +0000 | [diff] [blame] | 578 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 579 | static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) { |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 580 | // Ignore unknown pragmas. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 581 | PP.AddPragmaHandler(new EmptyPragmaHandler()); |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 582 | |
Eli Friedman | f1db585 | 2009-05-19 01:32:34 +0000 | [diff] [blame] | 583 | // -dM mode just scans and ignores all tokens in the files, then dumps out |
| 584 | // the macro table at the end. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 585 | PP.EnterMainSourceFile(); |
Eli Friedman | f1db585 | 2009-05-19 01:32:34 +0000 | [diff] [blame] | 586 | |
| 587 | Token Tok; |
| 588 | do PP.Lex(Tok); |
| 589 | while (Tok.isNot(tok::eof)); |
| 590 | |
Alexander Kornienko | 8a64bb5 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 591 | SmallVector<id_macro_pair, 128> MacrosByID; |
| 592 | for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); |
| 593 | I != E; ++I) { |
| 594 | if (I->first->hasMacroDefinition()) |
| 595 | MacrosByID.push_back(id_macro_pair(I->first, I->second)); |
| 596 | } |
Benjamin Kramer | 3ff2a4b | 2010-01-19 17:42:20 +0000 | [diff] [blame] | 597 | llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare); |
Eli Friedman | f1db585 | 2009-05-19 01:32:34 +0000 | [diff] [blame] | 598 | |
| 599 | for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) { |
| 600 | MacroInfo &MI = *MacrosByID[i].second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | // Ignore computed macros like __LINE__ and friends. |
Eli Friedman | f1db585 | 2009-05-19 01:32:34 +0000 | [diff] [blame] | 602 | if (MI.isBuiltinMacro()) continue; |
| 603 | |
| 604 | PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS); |
Benjamin Kramer | 3ff2a4b | 2010-01-19 17:42:20 +0000 | [diff] [blame] | 605 | *OS << '\n'; |
Eli Friedman | f1db585 | 2009-05-19 01:32:34 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Chris Lattner | 59076ab | 2009-02-06 05:56:11 +0000 | [diff] [blame] | 609 | /// DoPrintPreprocessedInput - This implements -E mode. |
| 610 | /// |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 611 | void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, |
Daniel Dunbar | 29cf746 | 2009-11-11 10:07:44 +0000 | [diff] [blame] | 612 | const PreprocessorOutputOptions &Opts) { |
Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 613 | // Show macros with no output is handled specially. |
| 614 | if (!Opts.ShowCPP) { |
| 615 | assert(Opts.ShowMacros && "Not yet implemented!"); |
| 616 | DoPrintMacros(PP, OS); |
| 617 | return; |
| 618 | } |
| 619 | |
Chris Lattner | 59076ab | 2009-02-06 05:56:11 +0000 | [diff] [blame] | 620 | // Inform the preprocessor whether we want it to retain comments or not, due |
| 621 | // to -C or -CC. |
Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 622 | PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments); |
Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 623 | |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 624 | PrintPPOutputPPCallbacks *Callbacks = |
Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 625 | new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers, |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 626 | Opts.ShowMacros); |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 627 | PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks)); |
Chris Lattner | 1397399 | 2010-11-10 01:00:49 +0000 | [diff] [blame] | 628 | PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks)); |
| 629 | PP.AddPragmaHandler("clang", |
| 630 | new UnknownPragmaHandler("#pragma clang", Callbacks)); |
Chris Lattner | 59076ab | 2009-02-06 05:56:11 +0000 | [diff] [blame] | 631 | |
Kovarththanan Rajaratnam | a5d10c4 | 2010-03-07 07:30:06 +0000 | [diff] [blame] | 632 | PP.addPPCallbacks(Callbacks); |
Chris Lattner | 59076ab | 2009-02-06 05:56:11 +0000 | [diff] [blame] | 633 | |
Eli Friedman | f1db585 | 2009-05-19 01:32:34 +0000 | [diff] [blame] | 634 | // After we have configured the preprocessor, enter the main file. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 635 | PP.EnterMainSourceFile(); |
Chris Lattner | 59076ab | 2009-02-06 05:56:11 +0000 | [diff] [blame] | 636 | |
Eli Friedman | f1db585 | 2009-05-19 01:32:34 +0000 | [diff] [blame] | 637 | // Consume all of the tokens that come from the predefines buffer. Those |
| 638 | // should not be emitted into the output and are guaranteed to be at the |
| 639 | // start. |
| 640 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 641 | Token Tok; |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 642 | do { |
| 643 | PP.Lex(Tok); |
| 644 | if (Tok.is(tok::eof) || !Tok.getLocation().isFileID()) |
| 645 | break; |
| 646 | |
| 647 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 648 | if (PLoc.isInvalid()) |
| 649 | break; |
| 650 | |
| 651 | if (strcmp(PLoc.getFilename(), "<built-in>")) |
| 652 | break; |
| 653 | } while (true); |
Chris Lattner | 59076ab | 2009-02-06 05:56:11 +0000 | [diff] [blame] | 654 | |
Eli Friedman | f1db585 | 2009-05-19 01:32:34 +0000 | [diff] [blame] | 655 | // Read all the preprocessed tokens, printing them out to the stream. |
| 656 | PrintPreprocessedTokens(PP, Tok, Callbacks, *OS); |
| 657 | *OS << '\n'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 658 | } |