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