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