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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | #include "llvm/Config/config.h" |
Chris Lattner | dceb6a7 | 2008-08-17 01:47:12 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
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, |
| 36 | Preprocessor &PP, llvm::raw_ostream &OS) { |
| 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 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 66 | llvm::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: |
| 86 | llvm::raw_ostream &OS; |
| 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; |
Eli Friedman | 3e753e2 | 2009-06-02 07:55:39 +0000 | [diff] [blame] | 91 | bool EmittedMacroOnThisLine; |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 92 | SrcMgr::CharacteristicKind FileType; |
Chris Lattner | d8e3083 | 2007-07-24 06:57:14 +0000 | [diff] [blame] | 93 | llvm::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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | public: |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 99 | PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os, |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 100 | bool lineMarkers, bool defines) |
Chris Lattner | df72140 | 2010-04-13 00:06:42 +0000 | [diff] [blame] | 101 | : PP(pp), SM(PP.getSourceManager()), |
| 102 | ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers), |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 103 | DumpDefines(defines) { |
| 104 | CurLine = 0; |
Chris Lattner | d8e3083 | 2007-07-24 06:57:14 +0000 | [diff] [blame] | 105 | CurFilename += "<uninit>"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | EmittedTokensOnThisLine = false; |
Eli Friedman | 3e753e2 | 2009-06-02 07:55:39 +0000 | [diff] [blame] | 107 | EmittedMacroOnThisLine = false; |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 108 | FileType = SrcMgr::C_User; |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 109 | Initialized = false; |
Kovarththanan Rajaratnam | a5d10c4 | 2010-03-07 07:30:06 +0000 | [diff] [blame] | 110 | |
Chris Lattner | f744934 | 2009-12-07 01:42:56 +0000 | [diff] [blame] | 111 | // If we're in microsoft mode, use normal #line instead of line markers. |
| 112 | UseLineDirective = PP.getLangOptions().Microsoft; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } |
Chris Lattner | f0f2b29 | 2007-07-23 06:09:34 +0000 | [diff] [blame] | 116 | bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 118 | bool StartNewLineIfNeeded(); |
| 119 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 121 | SrcMgr::CharacteristicKind FileType); |
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 | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 125 | virtual void PragmaMessage(SourceLocation Loc, llvm::StringRef Str); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 126 | virtual void PragmaDiagnosticPush(SourceLocation Loc, |
| 127 | llvm::StringRef Namespace); |
| 128 | virtual void PragmaDiagnosticPop(SourceLocation Loc, |
| 129 | llvm::StringRef Namespace); |
| 130 | virtual void PragmaDiagnostic(SourceLocation Loc, llvm::StringRef Namespace, |
| 131 | diag::Mapping Map, llvm::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, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 238 | SrcMgr::CharacteristicKind NewFileType) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 240 | // #include directive was at. |
Chris Lattner | df72140 | 2010-04-13 00:06:42 +0000 | [diff] [blame] | 241 | SourceManager &SourceMgr = SM; |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 242 | |
| 243 | PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 244 | if (UserLoc.isInvalid()) |
| 245 | return; |
| 246 | |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 247 | unsigned NewLine = UserLoc.getLine(); |
Daniel Dunbar | f7c16d9 | 2010-08-24 22:44:13 +0000 | [diff] [blame] | 248 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | if (Reason == PPCallbacks::EnterFile) { |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 250 | SourceLocation IncludeLoc = UserLoc.getIncludeLoc(); |
Chris Lattner | 71d8bfb | 2009-01-30 18:44:17 +0000 | [diff] [blame] | 251 | if (IncludeLoc.isValid()) |
| 252 | MoveToLine(IncludeLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | } else if (Reason == PPCallbacks::SystemHeaderPragma) { |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 254 | MoveToLine(NewLine); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | // TODO GCC emits the # directive for this directive on the line AFTER the |
| 257 | // directive and emits a bunch of spaces that aren't needed. Emulate this |
| 258 | // strange behavior. |
| 259 | } |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 260 | |
| 261 | CurLine = NewLine; |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 262 | |
Chris Lattner | d8e3083 | 2007-07-24 06:57:14 +0000 | [diff] [blame] | 263 | CurFilename.clear(); |
Chris Lattner | 88aae91 | 2010-04-13 00:01:41 +0000 | [diff] [blame] | 264 | CurFilename += UserLoc.getFilename(); |
Chris Lattner | d8e3083 | 2007-07-24 06:57:14 +0000 | [diff] [blame] | 265 | Lexer::Stringify(CurFilename); |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 266 | FileType = NewFileType; |
| 267 | |
Daniel Dunbar | f7c16d9 | 2010-08-24 22:44:13 +0000 | [diff] [blame] | 268 | if (DisableLineMarkers) return; |
| 269 | |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 270 | if (!Initialized) { |
| 271 | WriteLineInfo(CurLine); |
| 272 | Initialized = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | } |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 274 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | switch (Reason) { |
| 276 | case PPCallbacks::EnterFile: |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 277 | WriteLineInfo(CurLine, " 1", 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | break; |
| 279 | case PPCallbacks::ExitFile: |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 280 | WriteLineInfo(CurLine, " 2", 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | case PPCallbacks::SystemHeaderPragma: |
| 283 | case PPCallbacks::RenameFile: |
Daniel Dunbar | 737bdb4 | 2008-09-05 03:22:57 +0000 | [diff] [blame] | 284 | WriteLineInfo(CurLine); |
| 285 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 289 | /// Ident - Handle #ident directives when read by the preprocessor. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | /// |
| 291 | void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) { |
| 292 | MoveToLine(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Chris Lattner | e96de3e | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 294 | OS.write("#ident ", strlen("#ident ")); |
| 295 | OS.write(&S[0], S.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | EmittedTokensOnThisLine = true; |
| 297 | } |
| 298 | |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 299 | /// MacroDefined - This hook is called whenever a macro definition is seen. |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 300 | void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 301 | const MacroInfo *MI) { |
| 302 | // Only print out macro definitions in -dD mode. |
| 303 | if (!DumpDefines || |
| 304 | // Ignore __FILE__ etc. |
| 305 | MI->isBuiltinMacro()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 307 | MoveToLine(MI->getDefinitionLoc()); |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 308 | PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS); |
Eli Friedman | 3e753e2 | 2009-06-02 07:55:39 +0000 | [diff] [blame] | 309 | EmittedMacroOnThisLine = true; |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 312 | void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, |
Benjamin Kramer | 2f05449 | 2010-08-07 22:27:00 +0000 | [diff] [blame] | 313 | const MacroInfo *MI) { |
| 314 | // Only print out macro definitions in -dD mode. |
| 315 | if (!DumpDefines) return; |
| 316 | |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 317 | MoveToLine(MacroNameTok.getLocation()); |
| 318 | OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName(); |
Benjamin Kramer | 2f05449 | 2010-08-07 22:27:00 +0000 | [diff] [blame] | 319 | EmittedMacroOnThisLine = true; |
| 320 | } |
Chris Lattner | d82df3a | 2009-04-12 01:56:53 +0000 | [diff] [blame] | 321 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 322 | void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | const IdentifierInfo *Kind, |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 324 | const std::string &Str) { |
| 325 | MoveToLine(Loc); |
| 326 | OS << "#pragma comment(" << Kind->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 328 | if (!Str.empty()) { |
| 329 | OS << ", \""; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 331 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 332 | unsigned char Char = Str[i]; |
Chris Lattner | 52a3e9e | 2009-01-16 22:13:37 +0000 | [diff] [blame] | 333 | if (isprint(Char) && Char != '\\' && Char != '"') |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 334 | OS << (char)Char; |
| 335 | else // Output anything hard as an octal escape. |
| 336 | OS << '\\' |
| 337 | << (char)('0'+ ((Char >> 6) & 7)) |
| 338 | << (char)('0'+ ((Char >> 3) & 7)) |
| 339 | << (char)('0'+ ((Char >> 0) & 7)); |
| 340 | } |
| 341 | OS << '"'; |
| 342 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Chris Lattner | c7d945d | 2009-01-16 19:25:54 +0000 | [diff] [blame] | 344 | OS << ')'; |
| 345 | EmittedTokensOnThisLine = true; |
| 346 | } |
| 347 | |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 348 | void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc, |
| 349 | llvm::StringRef Str) { |
| 350 | MoveToLine(Loc); |
| 351 | OS << "#pragma message("; |
| 352 | |
| 353 | OS << '"'; |
| 354 | |
| 355 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 356 | unsigned char Char = Str[i]; |
| 357 | if (isprint(Char) && Char != '\\' && Char != '"') |
| 358 | OS << (char)Char; |
| 359 | else // Output anything hard as an octal escape. |
| 360 | OS << '\\' |
| 361 | << (char)('0'+ ((Char >> 6) & 7)) |
| 362 | << (char)('0'+ ((Char >> 3) & 7)) |
| 363 | << (char)('0'+ ((Char >> 0) & 7)); |
| 364 | } |
| 365 | OS << '"'; |
| 366 | |
| 367 | OS << ')'; |
| 368 | EmittedTokensOnThisLine = true; |
| 369 | } |
| 370 | |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 371 | void PrintPPOutputPPCallbacks:: |
| 372 | PragmaDiagnosticPush(SourceLocation Loc, llvm::StringRef Namespace) { |
| 373 | MoveToLine(Loc); |
| 374 | OS << "#pragma " << Namespace << " diagnostic push"; |
| 375 | EmittedTokensOnThisLine = true; |
| 376 | } |
| 377 | |
| 378 | void PrintPPOutputPPCallbacks:: |
| 379 | PragmaDiagnosticPop(SourceLocation Loc, llvm::StringRef Namespace) { |
| 380 | MoveToLine(Loc); |
| 381 | OS << "#pragma " << Namespace << " diagnostic pop"; |
| 382 | EmittedTokensOnThisLine = true; |
| 383 | } |
| 384 | |
| 385 | void PrintPPOutputPPCallbacks:: |
| 386 | PragmaDiagnostic(SourceLocation Loc, llvm::StringRef Namespace, |
| 387 | diag::Mapping Map, llvm::StringRef Str) { |
| 388 | MoveToLine(Loc); |
| 389 | OS << "#pragma " << Namespace << " diagnostic "; |
| 390 | switch (Map) { |
| 391 | default: llvm_unreachable("unexpected diagnostic kind"); |
| 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. |
Chris Lattner | df72140 | 2010-04-13 00:06:42 +0000 | [diff] [blame] | 422 | unsigned ColNo = SM.getInstantiationColumnNumber(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, |
| 494 | llvm::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 | |
Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 553 | static void DoPrintMacros(Preprocessor &PP, llvm::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 | |
Benjamin Kramer | 3ff2a4b | 2010-01-19 17:42:20 +0000 | [diff] [blame] | 565 | llvm::SmallVector<id_macro_pair, 128> |
| 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 | /// |
Eli Friedman | 12d3b1d | 2009-05-19 03:06:47 +0000 | [diff] [blame] | 581 | void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::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 | } |