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