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