PrintPreprocessedOutput.cpp revision 4e4d08403ca5cfd4d558fa2936215d3a4e5a528d
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  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  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.getLangOpts().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  case diag::MAP_WARNING:
393    OS << "warning";
394    break;
395  case diag::MAP_ERROR:
396    OS << "error";
397    break;
398  case diag::MAP_IGNORE:
399    OS << "ignored";
400    break;
401  case diag::MAP_FATAL:
402    OS << "fatal";
403    break;
404  }
405  OS << " \"" << Str << '"';
406  EmittedTokensOnThisLine = true;
407}
408
409/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
410/// is called for the first token on each new line.  If this really is the start
411/// of a new logical line, handle it and return true, otherwise return false.
412/// This may not be the start of a logical line because the "start of line"
413/// marker is set for spelling lines, not expansion ones.
414bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
415  // Figure out what line we went to and insert the appropriate number of
416  // newline characters.
417  if (!MoveToLine(Tok.getLocation()))
418    return false;
419
420  // Print out space characters so that the first token on a line is
421  // indented for easy reading.
422  unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
423
424  // This hack prevents stuff like:
425  // #define HASH #
426  // HASH define foo bar
427  // From having the # character end up at column 1, which makes it so it
428  // is not handled as a #define next time through the preprocessor if in
429  // -fpreprocessed mode.
430  if (ColNo <= 1 && Tok.is(tok::hash))
431    OS << ' ';
432
433  // Otherwise, indent the appropriate number of spaces.
434  for (; ColNo > 1; --ColNo)
435    OS << ' ';
436
437  return true;
438}
439
440void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
441                                                     unsigned Len) {
442  unsigned NumNewlines = 0;
443  for (; Len; --Len, ++TokStr) {
444    if (*TokStr != '\n' &&
445        *TokStr != '\r')
446      continue;
447
448    ++NumNewlines;
449
450    // If we have \n\r or \r\n, skip both and count as one line.
451    if (Len != 1 &&
452        (TokStr[1] == '\n' || TokStr[1] == '\r') &&
453        TokStr[0] != TokStr[1])
454      ++TokStr, --Len;
455  }
456
457  if (NumNewlines == 0) return;
458
459  CurLine += NumNewlines;
460}
461
462
463namespace {
464struct UnknownPragmaHandler : public PragmaHandler {
465  const char *Prefix;
466  PrintPPOutputPPCallbacks *Callbacks;
467
468  UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
469    : Prefix(prefix), Callbacks(callbacks) {}
470  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
471                            Token &PragmaTok) {
472    // Figure out what line we went to and insert the appropriate number of
473    // newline characters.
474    Callbacks->StartNewLineIfNeeded();
475    Callbacks->MoveToLine(PragmaTok.getLocation());
476    Callbacks->OS.write(Prefix, strlen(Prefix));
477    Callbacks->SetEmittedTokensOnThisLine();
478    // Read and print all of the pragma tokens.
479    while (PragmaTok.isNot(tok::eod)) {
480      if (PragmaTok.hasLeadingSpace())
481        Callbacks->OS << ' ';
482      std::string TokSpell = PP.getSpelling(PragmaTok);
483      Callbacks->OS.write(&TokSpell[0], TokSpell.size());
484      PP.LexUnexpandedToken(PragmaTok);
485    }
486    Callbacks->StartNewLineIfNeeded();
487  }
488};
489} // end anonymous namespace
490
491
492static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
493                                    PrintPPOutputPPCallbacks *Callbacks,
494                                    raw_ostream &OS) {
495  char Buffer[256];
496  Token PrevPrevTok, PrevTok;
497  PrevPrevTok.startToken();
498  PrevTok.startToken();
499  while (1) {
500
501    // If this token is at the start of a line, emit newlines if needed.
502    if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
503      // done.
504    } else if (Tok.hasLeadingSpace() ||
505               // If we haven't emitted a token on this line yet, PrevTok isn't
506               // useful to look at and no concatenation could happen anyway.
507               (Callbacks->hasEmittedTokensOnThisLine() &&
508                // Don't print "-" next to "-", it would form "--".
509                Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
510      OS << ' ';
511    }
512
513    if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
514      OS << II->getName();
515    } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
516               Tok.getLiteralData()) {
517      OS.write(Tok.getLiteralData(), Tok.getLength());
518    } else if (Tok.getLength() < 256) {
519      const char *TokPtr = Buffer;
520      unsigned Len = PP.getSpelling(Tok, TokPtr);
521      OS.write(TokPtr, Len);
522
523      // Tokens that can contain embedded newlines need to adjust our current
524      // line number.
525      if (Tok.getKind() == tok::comment)
526        Callbacks->HandleNewlinesInToken(TokPtr, Len);
527    } else {
528      std::string S = PP.getSpelling(Tok);
529      OS.write(&S[0], S.size());
530
531      // Tokens that can contain embedded newlines need to adjust our current
532      // line number.
533      if (Tok.getKind() == tok::comment)
534        Callbacks->HandleNewlinesInToken(&S[0], S.size());
535    }
536    Callbacks->SetEmittedTokensOnThisLine();
537
538    if (Tok.is(tok::eof)) break;
539
540    PrevPrevTok = PrevTok;
541    PrevTok = Tok;
542    PP.Lex(Tok);
543  }
544}
545
546typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
547static int MacroIDCompare(const void* a, const void* b) {
548  const id_macro_pair *LHS = static_cast<const id_macro_pair*>(a);
549  const id_macro_pair *RHS = static_cast<const id_macro_pair*>(b);
550  return LHS->first->getName().compare(RHS->first->getName());
551}
552
553static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
554  // Ignore unknown pragmas.
555  PP.AddPragmaHandler(new EmptyPragmaHandler());
556
557  // -dM mode just scans and ignores all tokens in the files, then dumps out
558  // the macro table at the end.
559  PP.EnterMainSourceFile();
560
561  Token Tok;
562  do PP.Lex(Tok);
563  while (Tok.isNot(tok::eof));
564
565  SmallVector<id_macro_pair, 128>
566    MacrosByID(PP.macro_begin(), PP.macro_end());
567  llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
568
569  for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
570    MacroInfo &MI = *MacrosByID[i].second;
571    // Ignore computed macros like __LINE__ and friends.
572    if (MI.isBuiltinMacro()) continue;
573
574    PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
575    *OS << '\n';
576  }
577}
578
579/// DoPrintPreprocessedInput - This implements -E mode.
580///
581void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
582                                     const PreprocessorOutputOptions &Opts) {
583  // Show macros with no output is handled specially.
584  if (!Opts.ShowCPP) {
585    assert(Opts.ShowMacros && "Not yet implemented!");
586    DoPrintMacros(PP, OS);
587    return;
588  }
589
590  // Inform the preprocessor whether we want it to retain comments or not, due
591  // to -C or -CC.
592  PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
593
594  PrintPPOutputPPCallbacks *Callbacks =
595      new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
596                                   Opts.ShowMacros);
597  PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks));
598  PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
599  PP.AddPragmaHandler("clang",
600                      new UnknownPragmaHandler("#pragma clang", Callbacks));
601
602  PP.addPPCallbacks(Callbacks);
603
604  // After we have configured the preprocessor, enter the main file.
605  PP.EnterMainSourceFile();
606
607  // Consume all of the tokens that come from the predefines buffer.  Those
608  // should not be emitted into the output and are guaranteed to be at the
609  // start.
610  const SourceManager &SourceMgr = PP.getSourceManager();
611  Token Tok;
612  do {
613    PP.Lex(Tok);
614    if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
615      break;
616
617    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
618    if (PLoc.isInvalid())
619      break;
620
621    if (strcmp(PLoc.getFilename(), "<built-in>"))
622      break;
623  } while (true);
624
625  // Read all the preprocessed tokens, printing them out to the stream.
626  PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
627  *OS << '\n';
628}
629