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