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/CharInfo.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Frontend/PreprocessorOutputOptions.h"
20#include "clang/Lex/MacroInfo.h"
21#include "clang/Lex/PPCallbacks.h"
22#include "clang/Lex/Pragma.h"
23#include "clang/Lex/Preprocessor.h"
24#include "clang/Lex/TokenConcatenation.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ADT/SmallString.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30#include <cstdio>
31using namespace clang;
32
33/// PrintMacroDefinition - Print a macro definition in a form that will be
34/// properly accepted back as a definition.
35static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
36                                 Preprocessor &PP, raw_ostream &OS) {
37  OS << "#define " << II.getName();
38
39  if (MI.isFunctionLike()) {
40    OS << '(';
41    if (!MI.arg_empty()) {
42      MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
43      for (; AI+1 != E; ++AI) {
44        OS << (*AI)->getName();
45        OS << ',';
46      }
47
48      // Last argument.
49      if ((*AI)->getName() == "__VA_ARGS__")
50        OS << "...";
51      else
52        OS << (*AI)->getName();
53    }
54
55    if (MI.isGNUVarargs())
56      OS << "...";  // #define foo(x...)
57
58    OS << ')';
59  }
60
61  // GCC always emits a space, even if the macro body is empty.  However, do not
62  // want to emit two spaces if the first token has a leading space.
63  if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
64    OS << ' ';
65
66  SmallString<128> SpellingBuffer;
67  for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
68       I != E; ++I) {
69    if (I->hasLeadingSpace())
70      OS << ' ';
71
72    OS << PP.getSpelling(*I, SpellingBuffer);
73  }
74}
75
76//===----------------------------------------------------------------------===//
77// Preprocessed token printer
78//===----------------------------------------------------------------------===//
79
80namespace {
81class PrintPPOutputPPCallbacks : public PPCallbacks {
82  Preprocessor &PP;
83  SourceManager &SM;
84  TokenConcatenation ConcatInfo;
85public:
86  raw_ostream &OS;
87private:
88  unsigned CurLine;
89
90  bool EmittedTokensOnThisLine;
91  bool EmittedDirectiveOnThisLine;
92  SrcMgr::CharacteristicKind FileType;
93  SmallString<512> CurFilename;
94  bool Initialized;
95  bool DisableLineMarkers;
96  bool DumpDefines;
97  bool UseLineDirective;
98  bool IsFirstFileEntered;
99public:
100  PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os,
101                           bool lineMarkers, bool defines)
102     : PP(pp), SM(PP.getSourceManager()),
103       ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
104       DumpDefines(defines) {
105    CurLine = 0;
106    CurFilename += "<uninit>";
107    EmittedTokensOnThisLine = false;
108    EmittedDirectiveOnThisLine = false;
109    FileType = SrcMgr::C_User;
110    Initialized = false;
111    IsFirstFileEntered = false;
112
113    // If we're in microsoft mode, use normal #line instead of line markers.
114    UseLineDirective = PP.getLangOpts().MicrosoftExt;
115  }
116
117  void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
118  bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
119
120  void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
121  bool hasEmittedDirectiveOnThisLine() const {
122    return EmittedDirectiveOnThisLine;
123  }
124
125  bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true);
126
127  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
128                           SrcMgr::CharacteristicKind FileType,
129                           FileID PrevFID);
130  virtual void InclusionDirective(SourceLocation HashLoc,
131                                  const Token &IncludeTok,
132                                  StringRef FileName,
133                                  bool IsAngled,
134                                  CharSourceRange FilenameRange,
135                                  const FileEntry *File,
136                                  StringRef SearchPath,
137                                  StringRef RelativePath,
138                                  const Module *Imported);
139  virtual void Ident(SourceLocation Loc, const std::string &str);
140  virtual void PragmaCaptured(SourceLocation Loc, StringRef Str);
141  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
142                             const std::string &Str);
143  virtual void PragmaDetectMismatch(SourceLocation Loc,
144                                    const std::string &Name,
145                                    const std::string &Value);
146  virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace,
147                             PragmaMessageKind Kind, StringRef Str);
148  virtual void PragmaDebug(SourceLocation Loc, StringRef DebugType);
149  virtual void PragmaDiagnosticPush(SourceLocation Loc,
150                                    StringRef Namespace);
151  virtual void PragmaDiagnosticPop(SourceLocation Loc,
152                                   StringRef Namespace);
153  virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
154                                diag::Mapping Map, StringRef Str);
155  virtual void PragmaWarning(SourceLocation Loc, StringRef WarningSpec,
156                             ArrayRef<int> Ids);
157  virtual void PragmaWarningPush(SourceLocation Loc, int Level);
158  virtual void PragmaWarningPop(SourceLocation Loc);
159
160  bool HandleFirstTokOnLine(Token &Tok);
161
162  /// Move to the line of the provided source location. This will
163  /// return true if the output stream required adjustment or if
164  /// the requested location is on the first line.
165  bool MoveToLine(SourceLocation Loc) {
166    PresumedLoc PLoc = SM.getPresumedLoc(Loc);
167    if (PLoc.isInvalid())
168      return false;
169    return MoveToLine(PLoc.getLine()) || (PLoc.getLine() == 1);
170  }
171  bool MoveToLine(unsigned LineNo);
172
173  bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
174                   const Token &Tok) {
175    return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
176  }
177  void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
178  bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
179  void HandleNewlinesInToken(const char *TokStr, unsigned Len);
180
181  /// MacroDefined - This hook is called whenever a macro definition is seen.
182  void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD);
183
184  /// MacroUndefined - This hook is called whenever a macro #undef is seen.
185  void MacroUndefined(const Token &MacroNameTok, const MacroDirective *MD);
186};
187}  // end anonymous namespace
188
189void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
190                                             const char *Extra,
191                                             unsigned ExtraLen) {
192  startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
193
194  // Emit #line directives or GNU line markers depending on what mode we're in.
195  if (UseLineDirective) {
196    OS << "#line" << ' ' << LineNo << ' ' << '"';
197    OS.write_escaped(CurFilename);
198    OS << '"';
199  } else {
200    OS << '#' << ' ' << LineNo << ' ' << '"';
201    OS.write_escaped(CurFilename);
202    OS << '"';
203
204    if (ExtraLen)
205      OS.write(Extra, ExtraLen);
206
207    if (FileType == SrcMgr::C_System)
208      OS.write(" 3", 2);
209    else if (FileType == SrcMgr::C_ExternCSystem)
210      OS.write(" 3 4", 4);
211  }
212  OS << '\n';
213}
214
215/// MoveToLine - Move the output to the source line specified by the location
216/// object.  We can do this by emitting some number of \n's, or be emitting a
217/// #line directive.  This returns false if already at the specified line, true
218/// if some newlines were emitted.
219bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
220  // If this line is "close enough" to the original line, just print newlines,
221  // otherwise print a #line directive.
222  if (LineNo-CurLine <= 8) {
223    if (LineNo-CurLine == 1)
224      OS << '\n';
225    else if (LineNo == CurLine)
226      return false;    // Spelling line moved, but expansion line didn't.
227    else {
228      const char *NewLines = "\n\n\n\n\n\n\n\n";
229      OS.write(NewLines, LineNo-CurLine);
230    }
231  } else if (!DisableLineMarkers) {
232    // Emit a #line or line marker.
233    WriteLineInfo(LineNo, 0, 0);
234  } else {
235    // Okay, we're in -P mode, which turns off line markers.  However, we still
236    // need to emit a newline between tokens on different lines.
237    startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
238  }
239
240  CurLine = LineNo;
241  return true;
242}
243
244bool
245PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) {
246  if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
247    OS << '\n';
248    EmittedTokensOnThisLine = false;
249    EmittedDirectiveOnThisLine = false;
250    if (ShouldUpdateCurrentLine)
251      ++CurLine;
252    return true;
253  }
254
255  return false;
256}
257
258/// FileChanged - Whenever the preprocessor enters or exits a #include file
259/// it invokes this handler.  Update our conception of the current source
260/// position.
261void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
262                                           FileChangeReason Reason,
263                                       SrcMgr::CharacteristicKind NewFileType,
264                                       FileID PrevFID) {
265  // Unless we are exiting a #include, make sure to skip ahead to the line the
266  // #include directive was at.
267  SourceManager &SourceMgr = SM;
268
269  PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
270  if (UserLoc.isInvalid())
271    return;
272
273  unsigned NewLine = UserLoc.getLine();
274
275  if (Reason == PPCallbacks::EnterFile) {
276    SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
277    if (IncludeLoc.isValid())
278      MoveToLine(IncludeLoc);
279  } else if (Reason == PPCallbacks::SystemHeaderPragma) {
280    // GCC emits the # directive for this directive on the line AFTER the
281    // directive and emits a bunch of spaces that aren't needed. This is because
282    // otherwise we will emit a line marker for THIS line, which requires an
283    // extra blank line after the directive to avoid making all following lines
284    // off by one. We can do better by simply incrementing NewLine here.
285    NewLine += 1;
286  }
287
288  CurLine = NewLine;
289
290  CurFilename.clear();
291  CurFilename += UserLoc.getFilename();
292  FileType = NewFileType;
293
294  if (DisableLineMarkers) {
295    startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
296    return;
297  }
298
299  if (!Initialized) {
300    WriteLineInfo(CurLine);
301    Initialized = true;
302  }
303
304  // Do not emit an enter marker for the main file (which we expect is the first
305  // entered file). This matches gcc, and improves compatibility with some tools
306  // which track the # line markers as a way to determine when the preprocessed
307  // output is in the context of the main file.
308  if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) {
309    IsFirstFileEntered = true;
310    return;
311  }
312
313  switch (Reason) {
314  case PPCallbacks::EnterFile:
315    WriteLineInfo(CurLine, " 1", 2);
316    break;
317  case PPCallbacks::ExitFile:
318    WriteLineInfo(CurLine, " 2", 2);
319    break;
320  case PPCallbacks::SystemHeaderPragma:
321  case PPCallbacks::RenameFile:
322    WriteLineInfo(CurLine);
323    break;
324  }
325}
326
327void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc,
328                                                  const Token &IncludeTok,
329                                                  StringRef FileName,
330                                                  bool IsAngled,
331                                                  CharSourceRange FilenameRange,
332                                                  const FileEntry *File,
333                                                  StringRef SearchPath,
334                                                  StringRef RelativePath,
335                                                  const Module *Imported) {
336  // When preprocessing, turn implicit imports into @imports.
337  // FIXME: This is a stop-gap until a more comprehensive "preprocessing with
338  // modules" solution is introduced.
339  if (Imported) {
340    startNewLineIfNeeded();
341    MoveToLine(HashLoc);
342    OS << "@import " << Imported->getFullModuleName() << ";"
343       << " /* clang -E: implicit import for \"" << File->getName() << "\" */";
344    EmittedTokensOnThisLine = true;
345  }
346}
347
348/// Ident - Handle #ident directives when read by the preprocessor.
349///
350void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
351  MoveToLine(Loc);
352
353  OS.write("#ident ", strlen("#ident "));
354  OS.write(&S[0], S.size());
355  EmittedTokensOnThisLine = true;
356}
357
358void PrintPPOutputPPCallbacks::PragmaCaptured(SourceLocation Loc,
359                                              StringRef Str) {
360  startNewLineIfNeeded();
361  MoveToLine(Loc);
362  OS << "#pragma captured";
363
364  setEmittedDirectiveOnThisLine();
365}
366
367/// MacroDefined - This hook is called whenever a macro definition is seen.
368void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
369                                            const MacroDirective *MD) {
370  const MacroInfo *MI = MD->getMacroInfo();
371  // Only print out macro definitions in -dD mode.
372  if (!DumpDefines ||
373      // Ignore __FILE__ etc.
374      MI->isBuiltinMacro()) return;
375
376  MoveToLine(MI->getDefinitionLoc());
377  PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
378  setEmittedDirectiveOnThisLine();
379}
380
381void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
382                                              const MacroDirective *MD) {
383  // Only print out macro definitions in -dD mode.
384  if (!DumpDefines) return;
385
386  MoveToLine(MacroNameTok.getLocation());
387  OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
388  setEmittedDirectiveOnThisLine();
389}
390
391static void outputPrintable(llvm::raw_ostream& OS,
392                                             const std::string &Str) {
393    for (unsigned i = 0, e = Str.size(); i != e; ++i) {
394      unsigned char Char = Str[i];
395      if (isPrintable(Char) && Char != '\\' && Char != '"')
396        OS << (char)Char;
397      else  // Output anything hard as an octal escape.
398        OS << '\\'
399           << (char)('0'+ ((Char >> 6) & 7))
400           << (char)('0'+ ((Char >> 3) & 7))
401           << (char)('0'+ ((Char >> 0) & 7));
402    }
403}
404
405void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
406                                             const IdentifierInfo *Kind,
407                                             const std::string &Str) {
408  startNewLineIfNeeded();
409  MoveToLine(Loc);
410  OS << "#pragma comment(" << Kind->getName();
411
412  if (!Str.empty()) {
413    OS << ", \"";
414    outputPrintable(OS, Str);
415    OS << '"';
416  }
417
418  OS << ')';
419  setEmittedDirectiveOnThisLine();
420}
421
422void PrintPPOutputPPCallbacks::PragmaDetectMismatch(SourceLocation Loc,
423                                                    const std::string &Name,
424                                                    const std::string &Value) {
425  startNewLineIfNeeded();
426  MoveToLine(Loc);
427  OS << "#pragma detect_mismatch(\"" << Name << '"';
428  outputPrintable(OS, Name);
429  OS << "\", \"";
430  outputPrintable(OS, Value);
431  OS << "\")";
432  setEmittedDirectiveOnThisLine();
433}
434
435void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
436                                             StringRef Namespace,
437                                             PragmaMessageKind Kind,
438                                             StringRef Str) {
439  startNewLineIfNeeded();
440  MoveToLine(Loc);
441  OS << "#pragma ";
442  if (!Namespace.empty())
443    OS << Namespace << ' ';
444  switch (Kind) {
445    case PMK_Message:
446      OS << "message(\"";
447      break;
448    case PMK_Warning:
449      OS << "warning \"";
450      break;
451    case PMK_Error:
452      OS << "error \"";
453      break;
454  }
455
456  outputPrintable(OS, Str);
457  OS << '"';
458  if (Kind == PMK_Message)
459    OS << ')';
460  setEmittedDirectiveOnThisLine();
461}
462
463void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
464                                           StringRef DebugType) {
465  startNewLineIfNeeded();
466  MoveToLine(Loc);
467
468  OS << "#pragma clang __debug ";
469  OS << DebugType;
470
471  setEmittedDirectiveOnThisLine();
472}
473
474void PrintPPOutputPPCallbacks::
475PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
476  startNewLineIfNeeded();
477  MoveToLine(Loc);
478  OS << "#pragma " << Namespace << " diagnostic push";
479  setEmittedDirectiveOnThisLine();
480}
481
482void PrintPPOutputPPCallbacks::
483PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
484  startNewLineIfNeeded();
485  MoveToLine(Loc);
486  OS << "#pragma " << Namespace << " diagnostic pop";
487  setEmittedDirectiveOnThisLine();
488}
489
490void PrintPPOutputPPCallbacks::
491PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
492                 diag::Mapping Map, StringRef Str) {
493  startNewLineIfNeeded();
494  MoveToLine(Loc);
495  OS << "#pragma " << Namespace << " diagnostic ";
496  switch (Map) {
497  case diag::MAP_WARNING:
498    OS << "warning";
499    break;
500  case diag::MAP_ERROR:
501    OS << "error";
502    break;
503  case diag::MAP_IGNORE:
504    OS << "ignored";
505    break;
506  case diag::MAP_FATAL:
507    OS << "fatal";
508    break;
509  }
510  OS << " \"" << Str << '"';
511  setEmittedDirectiveOnThisLine();
512}
513
514void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
515                                             StringRef WarningSpec,
516                                             ArrayRef<int> Ids) {
517  startNewLineIfNeeded();
518  MoveToLine(Loc);
519  OS << "#pragma warning(" << WarningSpec << ':';
520  for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I)
521    OS << ' ' << *I;
522  OS << ')';
523  setEmittedDirectiveOnThisLine();
524}
525
526void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc,
527                                                 int Level) {
528  startNewLineIfNeeded();
529  MoveToLine(Loc);
530  OS << "#pragma warning(push";
531  if (Level >= 0)
532    OS << ", " << Level;
533  OS << ')';
534  setEmittedDirectiveOnThisLine();
535}
536
537void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
538  startNewLineIfNeeded();
539  MoveToLine(Loc);
540  OS << "#pragma warning(pop)";
541  setEmittedDirectiveOnThisLine();
542}
543
544/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
545/// is called for the first token on each new line.  If this really is the start
546/// of a new logical line, handle it and return true, otherwise return false.
547/// This may not be the start of a logical line because the "start of line"
548/// marker is set for spelling lines, not expansion ones.
549bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
550  // Figure out what line we went to and insert the appropriate number of
551  // newline characters.
552  if (!MoveToLine(Tok.getLocation()))
553    return false;
554
555  // Print out space characters so that the first token on a line is
556  // indented for easy reading.
557  unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
558
559  // This hack prevents stuff like:
560  // #define HASH #
561  // HASH define foo bar
562  // From having the # character end up at column 1, which makes it so it
563  // is not handled as a #define next time through the preprocessor if in
564  // -fpreprocessed mode.
565  if (ColNo <= 1 && Tok.is(tok::hash))
566    OS << ' ';
567
568  // Otherwise, indent the appropriate number of spaces.
569  for (; ColNo > 1; --ColNo)
570    OS << ' ';
571
572  return true;
573}
574
575void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
576                                                     unsigned Len) {
577  unsigned NumNewlines = 0;
578  for (; Len; --Len, ++TokStr) {
579    if (*TokStr != '\n' &&
580        *TokStr != '\r')
581      continue;
582
583    ++NumNewlines;
584
585    // If we have \n\r or \r\n, skip both and count as one line.
586    if (Len != 1 &&
587        (TokStr[1] == '\n' || TokStr[1] == '\r') &&
588        TokStr[0] != TokStr[1])
589      ++TokStr, --Len;
590  }
591
592  if (NumNewlines == 0) return;
593
594  CurLine += NumNewlines;
595}
596
597
598namespace {
599struct UnknownPragmaHandler : public PragmaHandler {
600  const char *Prefix;
601  PrintPPOutputPPCallbacks *Callbacks;
602
603  UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
604    : Prefix(prefix), Callbacks(callbacks) {}
605  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
606                            Token &PragmaTok) {
607    // Figure out what line we went to and insert the appropriate number of
608    // newline characters.
609    Callbacks->startNewLineIfNeeded();
610    Callbacks->MoveToLine(PragmaTok.getLocation());
611    Callbacks->OS.write(Prefix, strlen(Prefix));
612    // Read and print all of the pragma tokens.
613    while (PragmaTok.isNot(tok::eod)) {
614      if (PragmaTok.hasLeadingSpace())
615        Callbacks->OS << ' ';
616      std::string TokSpell = PP.getSpelling(PragmaTok);
617      Callbacks->OS.write(&TokSpell[0], TokSpell.size());
618      PP.LexUnexpandedToken(PragmaTok);
619    }
620    Callbacks->setEmittedDirectiveOnThisLine();
621  }
622};
623} // end anonymous namespace
624
625
626static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
627                                    PrintPPOutputPPCallbacks *Callbacks,
628                                    raw_ostream &OS) {
629  bool DropComments = PP.getLangOpts().TraditionalCPP &&
630                      !PP.getCommentRetentionState();
631
632  char Buffer[256];
633  Token PrevPrevTok, PrevTok;
634  PrevPrevTok.startToken();
635  PrevTok.startToken();
636  while (1) {
637    if (Callbacks->hasEmittedDirectiveOnThisLine()) {
638      Callbacks->startNewLineIfNeeded();
639      Callbacks->MoveToLine(Tok.getLocation());
640    }
641
642    // If this token is at the start of a line, emit newlines if needed.
643    if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
644      // done.
645    } else if (Tok.hasLeadingSpace() ||
646               // If we haven't emitted a token on this line yet, PrevTok isn't
647               // useful to look at and no concatenation could happen anyway.
648               (Callbacks->hasEmittedTokensOnThisLine() &&
649                // Don't print "-" next to "-", it would form "--".
650                Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
651      OS << ' ';
652    }
653
654    if (DropComments && Tok.is(tok::comment)) {
655      // Skip comments. Normally the preprocessor does not generate
656      // tok::comment nodes at all when not keeping comments, but under
657      // -traditional-cpp the lexer keeps /all/ whitespace, including comments.
658      SourceLocation StartLoc = Tok.getLocation();
659      Callbacks->MoveToLine(StartLoc.getLocWithOffset(Tok.getLength()));
660    } else if (Tok.is(tok::annot_module_include)) {
661      // PrintPPOutputPPCallbacks::InclusionDirective handles producing
662      // appropriate output here. Ignore this token entirely.
663      PP.Lex(Tok);
664      continue;
665    } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
666      OS << II->getName();
667    } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
668               Tok.getLiteralData()) {
669      OS.write(Tok.getLiteralData(), Tok.getLength());
670    } else if (Tok.getLength() < 256) {
671      const char *TokPtr = Buffer;
672      unsigned Len = PP.getSpelling(Tok, TokPtr);
673      OS.write(TokPtr, Len);
674
675      // Tokens that can contain embedded newlines need to adjust our current
676      // line number.
677      if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
678        Callbacks->HandleNewlinesInToken(TokPtr, Len);
679    } else {
680      std::string S = PP.getSpelling(Tok);
681      OS.write(&S[0], S.size());
682
683      // Tokens that can contain embedded newlines need to adjust our current
684      // line number.
685      if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
686        Callbacks->HandleNewlinesInToken(&S[0], S.size());
687    }
688    Callbacks->setEmittedTokensOnThisLine();
689
690    if (Tok.is(tok::eof)) break;
691
692    PrevPrevTok = PrevTok;
693    PrevTok = Tok;
694    PP.Lex(Tok);
695  }
696}
697
698typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair;
699static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) {
700  return LHS->first->getName().compare(RHS->first->getName());
701}
702
703static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
704  // Ignore unknown pragmas.
705  PP.AddPragmaHandler(new EmptyPragmaHandler());
706
707  // -dM mode just scans and ignores all tokens in the files, then dumps out
708  // the macro table at the end.
709  PP.EnterMainSourceFile();
710
711  Token Tok;
712  do PP.Lex(Tok);
713  while (Tok.isNot(tok::eof));
714
715  SmallVector<id_macro_pair, 128> MacrosByID;
716  for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
717       I != E; ++I) {
718    if (I->first->hasMacroDefinition())
719      MacrosByID.push_back(id_macro_pair(I->first, I->second->getMacroInfo()));
720  }
721  llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
722
723  for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
724    MacroInfo &MI = *MacrosByID[i].second;
725    // Ignore computed macros like __LINE__ and friends.
726    if (MI.isBuiltinMacro()) continue;
727
728    PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
729    *OS << '\n';
730  }
731}
732
733/// DoPrintPreprocessedInput - This implements -E mode.
734///
735void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
736                                     const PreprocessorOutputOptions &Opts) {
737  // Show macros with no output is handled specially.
738  if (!Opts.ShowCPP) {
739    assert(Opts.ShowMacros && "Not yet implemented!");
740    DoPrintMacros(PP, OS);
741    return;
742  }
743
744  // Inform the preprocessor whether we want it to retain comments or not, due
745  // to -C or -CC.
746  PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
747
748  PrintPPOutputPPCallbacks *Callbacks =
749      new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
750                                   Opts.ShowMacros);
751  PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks));
752  PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
753  PP.AddPragmaHandler("clang",
754                      new UnknownPragmaHandler("#pragma clang", Callbacks));
755
756  PP.addPPCallbacks(Callbacks);
757
758  // After we have configured the preprocessor, enter the main file.
759  PP.EnterMainSourceFile();
760
761  // Consume all of the tokens that come from the predefines buffer.  Those
762  // should not be emitted into the output and are guaranteed to be at the
763  // start.
764  const SourceManager &SourceMgr = PP.getSourceManager();
765  Token Tok;
766  do {
767    PP.Lex(Tok);
768    if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
769      break;
770
771    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
772    if (PLoc.isInvalid())
773      break;
774
775    if (strcmp(PLoc.getFilename(), "<built-in>"))
776      break;
777  } while (true);
778
779  // Read all the preprocessed tokens, printing them out to the stream.
780  PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
781  *OS << '\n';
782}
783