PrintPreprocessedOutput.cpp revision 1770e0dadcab966990cca4d7516253c886fd907c
1//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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/Frontend/Utils.h"
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Frontend/PreprocessorOutputOptions.h"
19#include "clang/Lex/MacroInfo.h"
20#include "clang/Lex/PPCallbacks.h"
21#include "clang/Lex/Pragma.h"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Lex/TokenConcatenation.h"
24#include "llvm/ADT/SmallString.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/Support/ErrorHandling.h"
29#include <cstdio>
30using namespace clang;
31
32/// PrintMacroDefinition - Print a macro definition in a form that will be
33/// properly accepted back as a definition.
34static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
35                                 Preprocessor &PP, raw_ostream &OS) {
36  OS << "#define " << II.getName();
37
38  if (MI.isFunctionLike()) {
39    OS << '(';
40    if (!MI.arg_empty()) {
41      MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
42      for (; AI+1 != E; ++AI) {
43        OS << (*AI)->getName();
44        OS << ',';
45      }
46
47      // Last argument.
48      if ((*AI)->getName() == "__VA_ARGS__")
49        OS << "...";
50      else
51        OS << (*AI)->getName();
52    }
53
54    if (MI.isGNUVarargs())
55      OS << "...";  // #define foo(x...)
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::SmallString<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    OS << PP.getSpelling(*I, SpellingBuffer);
72  }
73}
74
75//===----------------------------------------------------------------------===//
76// Preprocessed token printer
77//===----------------------------------------------------------------------===//
78
79namespace {
80class PrintPPOutputPPCallbacks : public PPCallbacks {
81  Preprocessor &PP;
82  SourceManager &SM;
83  TokenConcatenation ConcatInfo;
84public:
85  raw_ostream &OS;
86private:
87  unsigned CurLine;
88
89  bool EmittedTokensOnThisLine;
90  bool EmittedMacroOnThisLine;
91  SrcMgr::CharacteristicKind FileType;
92  llvm::SmallString<512> CurFilename;
93  bool Initialized;
94  bool DisableLineMarkers;
95  bool DumpDefines;
96  bool UseLineDirective;
97public:
98  PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os,
99                           bool lineMarkers, bool defines)
100     : PP(pp), SM(PP.getSourceManager()),
101       ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
102       DumpDefines(defines) {
103    CurLine = 0;
104    CurFilename += "<uninit>";
105    EmittedTokensOnThisLine = false;
106    EmittedMacroOnThisLine = false;
107    FileType = SrcMgr::C_User;
108    Initialized = false;
109
110    // If we're in microsoft mode, use normal #line instead of line markers.
111    UseLineDirective = PP.getLangOptions().MicrosoftExt;
112  }
113
114  void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
115  bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
116
117  bool StartNewLineIfNeeded();
118
119  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
120                           SrcMgr::CharacteristicKind FileType,
121                           FileID PrevFID);
122  virtual void Ident(SourceLocation Loc, const std::string &str);
123  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
124                             const std::string &Str);
125  virtual void PragmaMessage(SourceLocation Loc, StringRef Str);
126  virtual void PragmaDiagnosticPush(SourceLocation Loc,
127                                    StringRef Namespace);
128  virtual void PragmaDiagnosticPop(SourceLocation Loc,
129                                   StringRef Namespace);
130  virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
131                                diag::Mapping Map, StringRef Str);
132
133  bool HandleFirstTokOnLine(Token &Tok);
134  bool MoveToLine(SourceLocation Loc) {
135    PresumedLoc PLoc = SM.getPresumedLoc(Loc);
136    if (PLoc.isInvalid())
137      return false;
138    return MoveToLine(PLoc.getLine());
139  }
140  bool MoveToLine(unsigned LineNo);
141
142  bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
143                   const Token &Tok) {
144    return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
145  }
146  void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
147  bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
148  void HandleNewlinesInToken(const char *TokStr, unsigned Len);
149
150  /// MacroDefined - This hook is called whenever a macro definition is seen.
151  void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI);
152
153  /// MacroUndefined - This hook is called whenever a macro #undef is seen.
154  void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI);
155};
156}  // end anonymous namespace
157
158void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
159                                             const char *Extra,
160                                             unsigned ExtraLen) {
161  if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
162    OS << '\n';
163    EmittedTokensOnThisLine = false;
164    EmittedMacroOnThisLine = false;
165  }
166
167  // Emit #line directives or GNU line markers depending on what mode we're in.
168  if (UseLineDirective) {
169    OS << "#line" << ' ' << LineNo << ' ' << '"';
170    OS.write(CurFilename.data(), CurFilename.size());
171    OS << '"';
172  } else {
173    OS << '#' << ' ' << LineNo << ' ' << '"';
174    OS.write(CurFilename.data(), CurFilename.size());
175    OS << '"';
176
177    if (ExtraLen)
178      OS.write(Extra, ExtraLen);
179
180    if (FileType == SrcMgr::C_System)
181      OS.write(" 3", 2);
182    else if (FileType == SrcMgr::C_ExternCSystem)
183      OS.write(" 3 4", 4);
184  }
185  OS << '\n';
186}
187
188/// MoveToLine - Move the output to the source line specified by the location
189/// object.  We can do this by emitting some number of \n's, or be emitting a
190/// #line directive.  This returns false if already at the specified line, true
191/// if some newlines were emitted.
192bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
193  // If this line is "close enough" to the original line, just print newlines,
194  // otherwise print a #line directive.
195  if (LineNo-CurLine <= 8) {
196    if (LineNo-CurLine == 1)
197      OS << '\n';
198    else if (LineNo == CurLine)
199      return false;    // Spelling line moved, but expansion line didn't.
200    else {
201      const char *NewLines = "\n\n\n\n\n\n\n\n";
202      OS.write(NewLines, LineNo-CurLine);
203    }
204  } else if (!DisableLineMarkers) {
205    // Emit a #line or line marker.
206    WriteLineInfo(LineNo, 0, 0);
207  } else {
208    // Okay, we're in -P mode, which turns off line markers.  However, we still
209    // need to emit a newline between tokens on different lines.
210    if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
211      OS << '\n';
212      EmittedTokensOnThisLine = false;
213      EmittedMacroOnThisLine = false;
214    }
215  }
216
217  CurLine = LineNo;
218  return true;
219}
220
221bool PrintPPOutputPPCallbacks::StartNewLineIfNeeded() {
222  if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
223    OS << '\n';
224    EmittedTokensOnThisLine = false;
225    EmittedMacroOnThisLine = false;
226    ++CurLine;
227    return true;
228  }
229
230  return false;
231}
232
233/// FileChanged - Whenever the preprocessor enters or exits a #include file
234/// it invokes this handler.  Update our conception of the current source
235/// position.
236void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
237                                           FileChangeReason Reason,
238                                       SrcMgr::CharacteristicKind NewFileType,
239                                       FileID PrevFID) {
240  // Unless we are exiting a #include, make sure to skip ahead to the line the
241  // #include directive was at.
242  SourceManager &SourceMgr = SM;
243
244  PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
245  if (UserLoc.isInvalid())
246    return;
247
248  unsigned NewLine = UserLoc.getLine();
249
250  if (Reason == PPCallbacks::EnterFile) {
251    SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
252    if (IncludeLoc.isValid())
253      MoveToLine(IncludeLoc);
254  } else if (Reason == PPCallbacks::SystemHeaderPragma) {
255    MoveToLine(NewLine);
256
257    // TODO GCC emits the # directive for this directive on the line AFTER the
258    // directive and emits a bunch of spaces that aren't needed.  Emulate this
259    // strange behavior.
260  }
261
262  CurLine = NewLine;
263
264  CurFilename.clear();
265  CurFilename += UserLoc.getFilename();
266  Lexer::Stringify(CurFilename);
267  FileType = NewFileType;
268
269  if (DisableLineMarkers) return;
270
271  if (!Initialized) {
272    WriteLineInfo(CurLine);
273    Initialized = true;
274  }
275
276  switch (Reason) {
277  case PPCallbacks::EnterFile:
278    WriteLineInfo(CurLine, " 1", 2);
279    break;
280  case PPCallbacks::ExitFile:
281    WriteLineInfo(CurLine, " 2", 2);
282    break;
283  case PPCallbacks::SystemHeaderPragma:
284  case PPCallbacks::RenameFile:
285    WriteLineInfo(CurLine);
286    break;
287  }
288}
289
290/// Ident - Handle #ident directives when read by the preprocessor.
291///
292void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
293  MoveToLine(Loc);
294
295  OS.write("#ident ", strlen("#ident "));
296  OS.write(&S[0], S.size());
297  EmittedTokensOnThisLine = true;
298}
299
300/// MacroDefined - This hook is called whenever a macro definition is seen.
301void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
302                                            const MacroInfo *MI) {
303  // Only print out macro definitions in -dD mode.
304  if (!DumpDefines ||
305      // Ignore __FILE__ etc.
306      MI->isBuiltinMacro()) return;
307
308  MoveToLine(MI->getDefinitionLoc());
309  PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
310  EmittedMacroOnThisLine = true;
311}
312
313void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
314                                              const MacroInfo *MI) {
315  // Only print out macro definitions in -dD mode.
316  if (!DumpDefines) return;
317
318  MoveToLine(MacroNameTok.getLocation());
319  OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
320  EmittedMacroOnThisLine = true;
321}
322
323void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
324                                             const IdentifierInfo *Kind,
325                                             const std::string &Str) {
326  MoveToLine(Loc);
327  OS << "#pragma comment(" << Kind->getName();
328
329  if (!Str.empty()) {
330    OS << ", \"";
331
332    for (unsigned i = 0, e = Str.size(); i != e; ++i) {
333      unsigned char Char = Str[i];
334      if (isprint(Char) && Char != '\\' && Char != '"')
335        OS << (char)Char;
336      else  // Output anything hard as an octal escape.
337        OS << '\\'
338           << (char)('0'+ ((Char >> 6) & 7))
339           << (char)('0'+ ((Char >> 3) & 7))
340           << (char)('0'+ ((Char >> 0) & 7));
341    }
342    OS << '"';
343  }
344
345  OS << ')';
346  EmittedTokensOnThisLine = true;
347}
348
349void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
350                                             StringRef Str) {
351  MoveToLine(Loc);
352  OS << "#pragma message(";
353
354  OS << '"';
355
356  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
357    unsigned char Char = Str[i];
358    if (isprint(Char) && Char != '\\' && Char != '"')
359      OS << (char)Char;
360    else  // Output anything hard as an octal escape.
361      OS << '\\'
362         << (char)('0'+ ((Char >> 6) & 7))
363         << (char)('0'+ ((Char >> 3) & 7))
364         << (char)('0'+ ((Char >> 0) & 7));
365  }
366  OS << '"';
367
368  OS << ')';
369  EmittedTokensOnThisLine = true;
370}
371
372void PrintPPOutputPPCallbacks::
373PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
374  MoveToLine(Loc);
375  OS << "#pragma " << Namespace << " diagnostic push";
376  EmittedTokensOnThisLine = true;
377}
378
379void PrintPPOutputPPCallbacks::
380PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
381  MoveToLine(Loc);
382  OS << "#pragma " << Namespace << " diagnostic pop";
383  EmittedTokensOnThisLine = true;
384}
385
386void PrintPPOutputPPCallbacks::
387PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
388                 diag::Mapping Map, StringRef Str) {
389  MoveToLine(Loc);
390  OS << "#pragma " << Namespace << " diagnostic ";
391  switch (Map) {
392  default: llvm_unreachable("unexpected diagnostic kind");
393  case diag::MAP_WARNING:
394    OS << "warning";
395    break;
396  case diag::MAP_ERROR:
397    OS << "error";
398    break;
399  case diag::MAP_IGNORE:
400    OS << "ignored";
401    break;
402  case diag::MAP_FATAL:
403    OS << "fatal";
404    break;
405  }
406  OS << " \"" << Str << '"';
407  EmittedTokensOnThisLine = true;
408}
409
410/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
411/// is called for the first token on each new line.  If this really is the start
412/// of a new logical line, handle it and return true, otherwise return false.
413/// This may not be the start of a logical line because the "start of line"
414/// marker is set for spelling lines, not expansion ones.
415bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
416  // Figure out what line we went to and insert the appropriate number of
417  // newline characters.
418  if (!MoveToLine(Tok.getLocation()))
419    return false;
420
421  // Print out space characters so that the first token on a line is
422  // indented for easy reading.
423  unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
424
425  // This hack prevents stuff like:
426  // #define HASH #
427  // HASH define foo bar
428  // From having the # character end up at column 1, which makes it so it
429  // is not handled as a #define next time through the preprocessor if in
430  // -fpreprocessed mode.
431  if (ColNo <= 1 && Tok.is(tok::hash))
432    OS << ' ';
433
434  // Otherwise, indent the appropriate number of spaces.
435  for (; ColNo > 1; --ColNo)
436    OS << ' ';
437
438  return true;
439}
440
441void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
442                                                     unsigned Len) {
443  unsigned NumNewlines = 0;
444  for (; Len; --Len, ++TokStr) {
445    if (*TokStr != '\n' &&
446        *TokStr != '\r')
447      continue;
448
449    ++NumNewlines;
450
451    // If we have \n\r or \r\n, skip both and count as one line.
452    if (Len != 1 &&
453        (TokStr[1] == '\n' || TokStr[1] == '\r') &&
454        TokStr[0] != TokStr[1])
455      ++TokStr, --Len;
456  }
457
458  if (NumNewlines == 0) return;
459
460  CurLine += NumNewlines;
461}
462
463
464namespace {
465struct UnknownPragmaHandler : public PragmaHandler {
466  const char *Prefix;
467  PrintPPOutputPPCallbacks *Callbacks;
468
469  UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
470    : Prefix(prefix), Callbacks(callbacks) {}
471  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
472                            Token &PragmaTok) {
473    // Figure out what line we went to and insert the appropriate number of
474    // newline characters.
475    Callbacks->StartNewLineIfNeeded();
476    Callbacks->MoveToLine(PragmaTok.getLocation());
477    Callbacks->OS.write(Prefix, strlen(Prefix));
478    Callbacks->SetEmittedTokensOnThisLine();
479    // Read and print all of the pragma tokens.
480    while (PragmaTok.isNot(tok::eod)) {
481      if (PragmaTok.hasLeadingSpace())
482        Callbacks->OS << ' ';
483      std::string TokSpell = PP.getSpelling(PragmaTok);
484      Callbacks->OS.write(&TokSpell[0], TokSpell.size());
485      PP.LexUnexpandedToken(PragmaTok);
486    }
487    Callbacks->StartNewLineIfNeeded();
488  }
489};
490} // end anonymous namespace
491
492
493static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
494                                    PrintPPOutputPPCallbacks *Callbacks,
495                                    raw_ostream &OS) {
496  char Buffer[256];
497  Token PrevPrevTok, PrevTok;
498  PrevPrevTok.startToken();
499  PrevTok.startToken();
500  while (1) {
501
502    // If this token is at the start of a line, emit newlines if needed.
503    if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
504      // done.
505    } else if (Tok.hasLeadingSpace() ||
506               // If we haven't emitted a token on this line yet, PrevTok isn't
507               // useful to look at and no concatenation could happen anyway.
508               (Callbacks->hasEmittedTokensOnThisLine() &&
509                // Don't print "-" next to "-", it would form "--".
510                Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
511      OS << ' ';
512    }
513
514    if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
515      OS << II->getName();
516    } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
517               Tok.getLiteralData()) {
518      OS.write(Tok.getLiteralData(), Tok.getLength());
519    } else if (Tok.getLength() < 256) {
520      const char *TokPtr = Buffer;
521      unsigned Len = PP.getSpelling(Tok, TokPtr);
522      OS.write(TokPtr, Len);
523
524      // Tokens that can contain embedded newlines need to adjust our current
525      // line number.
526      if (Tok.getKind() == tok::comment)
527        Callbacks->HandleNewlinesInToken(TokPtr, Len);
528    } else {
529      std::string S = PP.getSpelling(Tok);
530      OS.write(&S[0], S.size());
531
532      // Tokens that can contain embedded newlines need to adjust our current
533      // line number.
534      if (Tok.getKind() == tok::comment)
535        Callbacks->HandleNewlinesInToken(&S[0], S.size());
536    }
537    Callbacks->SetEmittedTokensOnThisLine();
538
539    if (Tok.is(tok::eof)) break;
540
541    PrevPrevTok = PrevTok;
542    PrevTok = Tok;
543    PP.Lex(Tok);
544  }
545}
546
547typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
548static int MacroIDCompare(const void* a, const void* b) {
549  const id_macro_pair *LHS = static_cast<const id_macro_pair*>(a);
550  const id_macro_pair *RHS = static_cast<const id_macro_pair*>(b);
551  return LHS->first->getName().compare(RHS->first->getName());
552}
553
554static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
555  // Ignore unknown pragmas.
556  PP.AddPragmaHandler(new EmptyPragmaHandler());
557
558  // -dM mode just scans and ignores all tokens in the files, then dumps out
559  // the macro table at the end.
560  PP.EnterMainSourceFile();
561
562  Token Tok;
563  do PP.Lex(Tok);
564  while (Tok.isNot(tok::eof));
565
566  SmallVector<id_macro_pair, 128>
567    MacrosByID(PP.macro_begin(), PP.macro_end());
568  llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
569
570  for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
571    MacroInfo &MI = *MacrosByID[i].second;
572    // Ignore computed macros like __LINE__ and friends.
573    if (MI.isBuiltinMacro()) continue;
574
575    PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
576    *OS << '\n';
577  }
578}
579
580/// DoPrintPreprocessedInput - This implements -E mode.
581///
582void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
583                                     const PreprocessorOutputOptions &Opts) {
584  // Show macros with no output is handled specially.
585  if (!Opts.ShowCPP) {
586    assert(Opts.ShowMacros && "Not yet implemented!");
587    DoPrintMacros(PP, OS);
588    return;
589  }
590
591  // Inform the preprocessor whether we want it to retain comments or not, due
592  // to -C or -CC.
593  PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
594
595  PrintPPOutputPPCallbacks *Callbacks =
596      new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
597                                   Opts.ShowMacros);
598  PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks));
599  PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
600  PP.AddPragmaHandler("clang",
601                      new UnknownPragmaHandler("#pragma clang", Callbacks));
602
603  PP.addPPCallbacks(Callbacks);
604
605  // After we have configured the preprocessor, enter the main file.
606  PP.EnterMainSourceFile();
607
608  // Consume all of the tokens that come from the predefines buffer.  Those
609  // should not be emitted into the output and are guaranteed to be at the
610  // start.
611  const SourceManager &SourceMgr = PP.getSourceManager();
612  Token Tok;
613  do {
614    PP.Lex(Tok);
615    if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
616      break;
617
618    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
619    if (PLoc.isInvalid())
620      break;
621
622    if (strcmp(PLoc.getFilename(), "<built-in>"))
623      break;
624  } while (true);
625
626  // Read all the preprocessed tokens, printing them out to the stream.
627  PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
628  *OS << '\n';
629}
630