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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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 | |
| 15 | #include "clang.h" |
| 16 | #include "clang/Lex/PPCallbacks.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Lex/Pragma.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/Config/config.h" |
| 23 | #include <cstdio> |
| 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Simple buffered I/O |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // |
| 30 | // Empirically, iostream is over 30% slower than stdio for this workload, and |
| 31 | // stdio itself isn't very well suited. The problem with stdio is use of |
| 32 | // putchar_unlocked. We have many newline characters that need to be emitted, |
| 33 | // but stdio needs to do extra checks to handle line buffering mode. These |
| 34 | // extra checks make putchar_unlocked fall off its inlined code path, hitting |
| 35 | // slow system code. In practice, using 'write' directly makes 'clang -E -P' |
| 36 | // about 10% faster than using the stdio path on darwin. |
| 37 | |
| 38 | #ifdef HAVE_UNISTD_H |
| 39 | #include <unistd.h> |
| 40 | #else |
| 41 | #define USE_STDIO 1 |
| 42 | #endif |
| 43 | |
| 44 | static char *OutBufStart = 0, *OutBufEnd, *OutBufCur; |
| 45 | |
| 46 | /// InitOutputBuffer - Initialize our output buffer. |
| 47 | /// |
| 48 | static void InitOutputBuffer() { |
| 49 | #ifndef USE_STDIO |
| 50 | OutBufStart = new char[64*1024]; |
| 51 | OutBufEnd = OutBufStart+64*1024; |
| 52 | OutBufCur = OutBufStart; |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | /// FlushBuffer - Write the accumulated bytes to the output stream. |
| 57 | /// |
| 58 | static void FlushBuffer() { |
| 59 | #ifndef USE_STDIO |
| 60 | write(STDOUT_FILENO, OutBufStart, OutBufCur-OutBufStart); |
| 61 | OutBufCur = OutBufStart; |
| 62 | #endif |
| 63 | } |
| 64 | |
| 65 | /// CleanupOutputBuffer - Finish up output. |
| 66 | /// |
| 67 | static void CleanupOutputBuffer() { |
| 68 | #ifndef USE_STDIO |
| 69 | FlushBuffer(); |
| 70 | delete [] OutBufStart; |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | static void OutputChar(char c) { |
| 75 | #ifdef USE_STDIO |
| 76 | putchar_unlocked(c); |
| 77 | #else |
| 78 | if (OutBufCur >= OutBufEnd) |
| 79 | FlushBuffer(); |
| 80 | *OutBufCur++ = c; |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | static void OutputString(const char *Ptr, unsigned Size) { |
| 85 | #ifdef USE_STDIO |
| 86 | fwrite(Ptr, Size, 1, stdout); |
| 87 | #else |
| 88 | if (OutBufCur+Size >= OutBufEnd) |
| 89 | FlushBuffer(); |
| 90 | memcpy(OutBufCur, Ptr, Size); |
| 91 | OutBufCur += Size; |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | // Preprocessed token printer |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
| 100 | static llvm::cl::opt<bool> |
| 101 | DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode")); |
| 102 | static llvm::cl::opt<bool> |
| 103 | EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode")); |
| 104 | static llvm::cl::opt<bool> |
| 105 | EnableMacroCommentOutput("CC", |
| 106 | llvm::cl::desc("Enable comment output in -E mode, " |
| 107 | "even from macro expansions")); |
| 108 | |
| 109 | namespace { |
| 110 | class PrintPPOutputPPCallbacks : public PPCallbacks { |
| 111 | Preprocessor &PP; |
| 112 | unsigned CurLine; |
| 113 | std::string CurFilename; |
| 114 | bool EmittedTokensOnThisLine; |
| 115 | DirectoryLookup::DirType FileType; |
| 116 | public: |
| 117 | PrintPPOutputPPCallbacks(Preprocessor &pp) : PP(pp) { |
| 118 | CurLine = 0; |
| 119 | CurFilename = "\"<uninit>\""; |
| 120 | EmittedTokensOnThisLine = false; |
| 121 | FileType = DirectoryLookup::NormalHeaderDir; |
| 122 | } |
| 123 | |
| 124 | void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } |
| 125 | |
| 126 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 127 | DirectoryLookup::DirType FileType); |
| 128 | virtual void Ident(SourceLocation Loc, const std::string &str); |
| 129 | |
| 130 | |
| 131 | void HandleFirstTokOnLine(LexerToken &Tok); |
| 132 | void MoveToLine(SourceLocation Loc); |
| 133 | bool AvoidConcat(const LexerToken &PrevTok, const LexerToken &Tok); |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | /// MoveToLine - Move the output to the source line specified by the location |
| 138 | /// object. We can do this by emitting some number of \n's, or be emitting a |
| 139 | /// #line directive. |
| 140 | void PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) { |
| 141 | if (DisableLineMarkers) { |
| 142 | if (EmittedTokensOnThisLine) { |
| 143 | OutputChar('\n'); |
| 144 | EmittedTokensOnThisLine = false; |
| 145 | } |
| 146 | return; |
| 147 | } |
| 148 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 149 | unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | |
| 151 | // If this line is "close enough" to the original line, just print newlines, |
| 152 | // otherwise print a #line directive. |
| 153 | if (LineNo-CurLine < 8) { |
| 154 | unsigned Line = CurLine; |
| 155 | for (; Line != LineNo; ++Line) |
| 156 | OutputChar('\n'); |
| 157 | CurLine = Line; |
| 158 | } else { |
| 159 | if (EmittedTokensOnThisLine) { |
| 160 | OutputChar('\n'); |
| 161 | EmittedTokensOnThisLine = false; |
| 162 | } |
| 163 | |
| 164 | CurLine = LineNo; |
| 165 | |
| 166 | OutputChar('#'); |
| 167 | OutputChar(' '); |
| 168 | std::string Num = llvm::utostr_32(LineNo); |
| 169 | OutputString(&Num[0], Num.size()); |
| 170 | OutputChar(' '); |
| 171 | OutputString(&CurFilename[0], CurFilename.size()); |
| 172 | |
| 173 | if (FileType == DirectoryLookup::SystemHeaderDir) |
| 174 | OutputString(" 3", 2); |
| 175 | else if (FileType == DirectoryLookup::ExternCSystemHeaderDir) |
| 176 | OutputString(" 3 4", 4); |
| 177 | OutputChar('\n'); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /// FileChanged - Whenever the preprocessor enters or exits a #include file |
| 183 | /// it invokes this handler. Update our conception of the current source |
| 184 | /// position. |
| 185 | void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, |
| 186 | FileChangeReason Reason, |
| 187 | DirectoryLookup::DirType FileType) { |
| 188 | if (DisableLineMarkers) return; |
| 189 | |
| 190 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 191 | // #include directive was at. |
| 192 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 193 | if (Reason == PPCallbacks::EnterFile) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 194 | MoveToLine(SourceMgr.getIncludeLoc(Loc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | } else if (Reason == PPCallbacks::SystemHeaderPragma) { |
| 196 | MoveToLine(Loc); |
| 197 | |
| 198 | // TODO GCC emits the # directive for this directive on the line AFTER the |
| 199 | // directive and emits a bunch of spaces that aren't needed. Emulate this |
| 200 | // strange behavior. |
| 201 | } |
| 202 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 203 | Loc = SourceMgr.getLogicalLoc(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | CurLine = SourceMgr.getLineNumber(Loc); |
| 205 | CurFilename = '"' + Lexer::Stringify(SourceMgr.getSourceName(Loc)) + '"'; |
| 206 | FileType = FileType; |
| 207 | |
| 208 | if (EmittedTokensOnThisLine) { |
| 209 | OutputChar('\n'); |
| 210 | EmittedTokensOnThisLine = false; |
| 211 | } |
| 212 | |
| 213 | if (DisableLineMarkers) return; |
| 214 | |
| 215 | OutputChar('#'); |
| 216 | OutputChar(' '); |
| 217 | std::string Num = llvm::utostr_32(CurLine); |
| 218 | OutputString(&Num[0], Num.size()); |
| 219 | OutputChar(' '); |
| 220 | OutputString(&CurFilename[0], CurFilename.size()); |
| 221 | |
| 222 | switch (Reason) { |
| 223 | case PPCallbacks::EnterFile: |
| 224 | OutputString(" 1", 2); |
| 225 | break; |
| 226 | case PPCallbacks::ExitFile: |
| 227 | OutputString(" 2", 2); |
| 228 | break; |
| 229 | case PPCallbacks::SystemHeaderPragma: break; |
| 230 | case PPCallbacks::RenameFile: break; |
| 231 | } |
| 232 | |
| 233 | if (FileType == DirectoryLookup::SystemHeaderDir) |
| 234 | OutputString(" 3", 2); |
| 235 | else if (FileType == DirectoryLookup::ExternCSystemHeaderDir) |
| 236 | OutputString(" 3 4", 4); |
| 237 | |
| 238 | OutputChar('\n'); |
| 239 | } |
| 240 | |
| 241 | /// HandleIdent - Handle #ident directives when read by the preprocessor. |
| 242 | /// |
| 243 | void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) { |
| 244 | MoveToLine(Loc); |
| 245 | |
| 246 | OutputString("#ident ", strlen("#ident ")); |
| 247 | OutputString(&S[0], S.size()); |
| 248 | EmittedTokensOnThisLine = true; |
| 249 | } |
| 250 | |
| 251 | /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this |
| 252 | /// is called for the first token on each new line. |
| 253 | void PrintPPOutputPPCallbacks::HandleFirstTokOnLine(LexerToken &Tok) { |
| 254 | // Figure out what line we went to and insert the appropriate number of |
| 255 | // newline characters. |
| 256 | MoveToLine(Tok.getLocation()); |
| 257 | |
| 258 | // Print out space characters so that the first token on a line is |
| 259 | // indented for easy reading. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 260 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 261 | unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | |
| 263 | // This hack prevents stuff like: |
| 264 | // #define HASH # |
| 265 | // HASH define foo bar |
| 266 | // From having the # character end up at column 1, which makes it so it |
| 267 | // is not handled as a #define next time through the preprocessor if in |
| 268 | // -fpreprocessed mode. |
| 269 | if (ColNo <= 1 && Tok.getKind() == tok::hash) |
| 270 | OutputChar(' '); |
| 271 | |
| 272 | // Otherwise, indent the appropriate number of spaces. |
| 273 | for (; ColNo > 1; --ColNo) |
| 274 | OutputChar(' '); |
| 275 | } |
| 276 | |
| 277 | namespace { |
| 278 | struct UnknownPragmaHandler : public PragmaHandler { |
| 279 | const char *Prefix; |
| 280 | PrintPPOutputPPCallbacks *Callbacks; |
| 281 | |
| 282 | UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks) |
| 283 | : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {} |
| 284 | virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) { |
| 285 | // Figure out what line we went to and insert the appropriate number of |
| 286 | // newline characters. |
| 287 | Callbacks->MoveToLine(PragmaTok.getLocation()); |
| 288 | OutputString(Prefix, strlen(Prefix)); |
| 289 | |
| 290 | // Read and print all of the pragma tokens. |
| 291 | while (PragmaTok.getKind() != tok::eom) { |
| 292 | if (PragmaTok.hasLeadingSpace()) |
| 293 | OutputChar(' '); |
| 294 | std::string TokSpell = PP.getSpelling(PragmaTok); |
| 295 | OutputString(&TokSpell[0], TokSpell.size()); |
| 296 | PP.LexUnexpandedToken(PragmaTok); |
| 297 | } |
| 298 | OutputChar('\n'); |
| 299 | } |
| 300 | }; |
| 301 | } // end anonymous namespace |
| 302 | |
| 303 | /// AvoidConcat - If printing PrevTok immediately followed by Tok would cause |
| 304 | /// the two individual tokens to be lexed as a single token, return true (which |
| 305 | /// causes a space to be printed between them). This allows the output of -E |
| 306 | /// mode to be lexed to the same token stream as lexing the input directly |
| 307 | /// would. |
| 308 | /// |
| 309 | /// This code must conservatively return true if it doesn't want to be 100% |
| 310 | /// accurate. This will cause the output to include extra space characters, but |
| 311 | /// the resulting output won't have incorrect concatenations going on. Examples |
| 312 | /// include "..", which we print with a space between, because we don't want to |
| 313 | /// track enough to tell "x.." from "...". |
| 314 | bool PrintPPOutputPPCallbacks::AvoidConcat(const LexerToken &PrevTok, |
| 315 | const LexerToken &Tok) { |
| 316 | char Buffer[256]; |
| 317 | |
| 318 | // If we haven't emitted a token on this line yet, PrevTok isn't useful to |
| 319 | // look at and no concatenation could happen anyway. |
| 320 | if (!EmittedTokensOnThisLine) |
| 321 | return false; |
| 322 | |
| 323 | // Basic algorithm: we look at the first character of the second token, and |
| 324 | // determine whether it, if appended to the first token, would form (or would |
| 325 | // contribute) to a larger token if concatenated. |
| 326 | char FirstChar; |
| 327 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 328 | // Avoid spelling identifiers, the most common form of token. |
| 329 | FirstChar = II->getName()[0]; |
| 330 | } else if (Tok.getLength() < 256) { |
| 331 | const char *TokPtr = Buffer; |
| 332 | PP.getSpelling(Tok, TokPtr); |
| 333 | FirstChar = TokPtr[0]; |
| 334 | } else { |
| 335 | FirstChar = PP.getSpelling(Tok)[0]; |
| 336 | } |
| 337 | |
| 338 | tok::TokenKind PrevKind = PrevTok.getKind(); |
| 339 | if (PrevTok.getIdentifierInfo()) // Language keyword or named operator. |
| 340 | PrevKind = tok::identifier; |
| 341 | |
| 342 | switch (PrevKind) { |
| 343 | default: return false; |
| 344 | case tok::identifier: // id+id or id+number or id+L"foo". |
| 345 | return isalnum(FirstChar) || FirstChar == '_'; |
| 346 | case tok::numeric_constant: |
| 347 | return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant || |
| 348 | FirstChar == '+' || FirstChar == '-' || FirstChar == '.'; |
| 349 | case tok::period: // ..., .*, .1234 |
| 350 | return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar); |
| 351 | case tok::amp: // &&, &= |
| 352 | return FirstChar == '&' || FirstChar == '='; |
| 353 | case tok::plus: // ++, += |
| 354 | return FirstChar == '+' || FirstChar == '='; |
| 355 | case tok::minus: // --, ->, -=, ->* |
| 356 | return FirstChar == '-' || FirstChar == '>' || FirstChar == '='; |
| 357 | case tok::slash: // /=, /*, // |
| 358 | return FirstChar == '=' || FirstChar == '*' || FirstChar == '/'; |
| 359 | case tok::less: // <<, <<=, <=, <?=, <?, <:, <% |
| 360 | return FirstChar == '<' || FirstChar == '?' || FirstChar == '=' || |
| 361 | FirstChar == ':' || FirstChar == '%'; |
| 362 | case tok::greater: // >>, >=, >>=, >?=, >?, ->* |
| 363 | return FirstChar == '>' || FirstChar == '?' || FirstChar == '=' || |
| 364 | FirstChar == '*'; |
| 365 | case tok::pipe: // ||, |= |
| 366 | return FirstChar == '|' || FirstChar == '='; |
| 367 | case tok::percent: // %=, %>, %: |
| 368 | return FirstChar == '=' || FirstChar == '>' || FirstChar == ':'; |
| 369 | case tok::colon: // ::, :> |
| 370 | return FirstChar == ':' || FirstChar == '>'; |
| 371 | case tok::hash: // ##, #@, %:%: |
| 372 | return FirstChar == '#' || FirstChar == '@' || FirstChar == '%'; |
| 373 | case tok::arrow: // ->* |
| 374 | return FirstChar == '*'; |
| 375 | |
| 376 | case tok::star: // *= |
| 377 | case tok::exclaim: // != |
| 378 | case tok::lessless: // <<= |
| 379 | case tok::greaterequal: // >>= |
| 380 | case tok::caret: // ^= |
| 381 | case tok::equal: // == |
| 382 | // Cases that concatenate only if the next char is =. |
| 383 | return FirstChar == '='; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /// DoPrintPreprocessedInput - This implements -E mode. |
| 388 | /// |
| 389 | void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP, |
| 390 | const LangOptions &Options) { |
| 391 | // Inform the preprocessor whether we want it to retain comments or not, due |
| 392 | // to -C or -CC. |
| 393 | PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput); |
| 394 | |
| 395 | InitOutputBuffer(); |
| 396 | |
| 397 | LexerToken Tok, PrevTok; |
| 398 | char Buffer[256]; |
| 399 | PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP); |
| 400 | PP.setPPCallbacks(Callbacks); |
| 401 | |
| 402 | PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks)); |
| 403 | PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks)); |
| 404 | |
| 405 | // After we have configured the preprocessor, enter the main file. |
| 406 | |
| 407 | // Start parsing the specified input file. |
| 408 | PP.EnterSourceFile(MainFileID, 0, true); |
| 409 | |
| 410 | do { |
| 411 | PrevTok = Tok; |
| 412 | PP.Lex(Tok); |
| 413 | |
| 414 | // If this token is at the start of a line, emit newlines if needed. |
| 415 | if (Tok.isAtStartOfLine()) { |
| 416 | Callbacks->HandleFirstTokOnLine(Tok); |
| 417 | } else if (Tok.hasLeadingSpace() || |
| 418 | // Don't print "-" next to "-", it would form "--". |
| 419 | Callbacks->AvoidConcat(PrevTok, Tok)) { |
| 420 | OutputChar(' '); |
| 421 | } |
| 422 | |
| 423 | if (Tok.getLength() < 256) { |
| 424 | const char *TokPtr = Buffer; |
| 425 | unsigned Len = PP.getSpelling(Tok, TokPtr); |
| 426 | OutputString(TokPtr, Len); |
| 427 | } else { |
| 428 | std::string S = PP.getSpelling(Tok); |
| 429 | OutputString(&S[0], S.size()); |
| 430 | } |
| 431 | Callbacks->SetEmittedTokensOnThisLine(); |
| 432 | } while (Tok.getKind() != tok::eof); |
| 433 | OutputChar('\n'); |
| 434 | |
| 435 | CleanupOutputBuffer(); |
| 436 | } |
| 437 | |