Preprocessor.cpp revision af8fa25c0d4e0540952a50bbd06dc1558954ccd9
1//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
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 file implements the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// Options to support:
15//   -H       - Print the name of each header file used.
16//   -d[DNI] - Dump various things.
17//   -fworking-directory - #line's with preprocessor's working dir.
18//   -fpreprocessed
19//   -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
20//   -W*
21//   -w
22//
23// Messages to emit:
24//   "Multiple include guards may be useful for:\n"
25//
26//===----------------------------------------------------------------------===//
27
28#include "clang/Lex/Preprocessor.h"
29#include "MacroArgs.h"
30#include "clang/Lex/ExternalPreprocessorSource.h"
31#include "clang/Lex/HeaderSearch.h"
32#include "clang/Lex/MacroInfo.h"
33#include "clang/Lex/Pragma.h"
34#include "clang/Lex/PreprocessingRecord.h"
35#include "clang/Lex/ScratchBuffer.h"
36#include "clang/Lex/LexDiagnostic.h"
37#include "clang/Lex/CodeCompletionHandler.h"
38#include "clang/Basic/SourceManager.h"
39#include "clang/Basic/FileManager.h"
40#include "clang/Basic/TargetInfo.h"
41#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/SmallVector.h"
43#include "llvm/Support/MemoryBuffer.h"
44#include "llvm/Support/raw_ostream.h"
45using namespace clang;
46
47//===----------------------------------------------------------------------===//
48ExternalPreprocessorSource::~ExternalPreprocessorSource() { }
49
50Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
51                           const TargetInfo &target, SourceManager &SM,
52                           HeaderSearch &Headers,
53                           IdentifierInfoLookup* IILookup,
54                           bool OwnsHeaders)
55  : Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()),
56    SourceMgr(SM), HeaderInfo(Headers), ExternalSource(0),
57    Identifiers(opts, IILookup), BuiltinInfo(Target), CodeComplete(0),
58    CodeCompletionFile(0), SkipMainFilePreamble(0, true), CurPPLexer(0),
59    CurDirLookup(0), Callbacks(0), MacroArgCache(0), Record(0), MIChainHead(0) {
60  ScratchBuf = new ScratchBuffer(SourceMgr);
61  CounterValue = 0; // __COUNTER__ starts at 0.
62  OwnsHeaderSearch = OwnsHeaders;
63
64  // Clear stats.
65  NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
66  NumIf = NumElse = NumEndif = 0;
67  NumEnteredSourceFiles = 0;
68  NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
69  NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
70  MaxIncludeStackDepth = 0;
71  NumSkipped = 0;
72
73  // Default to discarding comments.
74  KeepComments = false;
75  KeepMacroComments = false;
76
77  // Macro expansion is enabled.
78  DisableMacroExpansion = false;
79  InMacroArgs = false;
80  NumCachedTokenLexers = 0;
81
82  CachedLexPos = 0;
83
84  // We haven't read anything from the external source.
85  ReadMacrosFromExternalSource = false;
86
87  // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
88  // This gets unpoisoned where it is allowed.
89  (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
90
91  // Initialize the pragma handlers.
92  PragmaHandlers = new PragmaNamespace(llvm::StringRef());
93  RegisterBuiltinPragmas();
94
95  // Initialize builtin macros like __LINE__ and friends.
96  RegisterBuiltinMacros();
97}
98
99Preprocessor::~Preprocessor() {
100  assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
101
102  while (!IncludeMacroStack.empty()) {
103    delete IncludeMacroStack.back().TheLexer;
104    delete IncludeMacroStack.back().TheTokenLexer;
105    IncludeMacroStack.pop_back();
106  }
107
108  // Free any macro definitions.
109  for (MacroInfoChain *I = MIChainHead ; I ; ) {
110    MacroInfoChain *Next = I->Next;
111    I->MI.Destroy();
112    I = Next;
113  }
114
115  // Free any cached macro expanders.
116  for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
117    delete TokenLexerCache[i];
118
119  // Free any cached MacroArgs.
120  for (MacroArgs *ArgList = MacroArgCache; ArgList; )
121    ArgList = ArgList->deallocate();
122
123  // Release pragma information.
124  delete PragmaHandlers;
125
126  // Delete the scratch buffer info.
127  delete ScratchBuf;
128
129  // Delete the header search info, if we own it.
130  if (OwnsHeaderSearch)
131    delete &HeaderInfo;
132
133  delete Callbacks;
134}
135
136void Preprocessor::setPTHManager(PTHManager* pm) {
137  PTH.reset(pm);
138  FileMgr.addStatCache(PTH->createStatCache());
139}
140
141void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
142  llvm::errs() << tok::getTokenName(Tok.getKind()) << " '"
143               << getSpelling(Tok) << "'";
144
145  if (!DumpFlags) return;
146
147  llvm::errs() << "\t";
148  if (Tok.isAtStartOfLine())
149    llvm::errs() << " [StartOfLine]";
150  if (Tok.hasLeadingSpace())
151    llvm::errs() << " [LeadingSpace]";
152  if (Tok.isExpandDisabled())
153    llvm::errs() << " [ExpandDisabled]";
154  if (Tok.needsCleaning()) {
155    const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
156    llvm::errs() << " [UnClean='" << llvm::StringRef(Start, Tok.getLength())
157                 << "']";
158  }
159
160  llvm::errs() << "\tLoc=<";
161  DumpLocation(Tok.getLocation());
162  llvm::errs() << ">";
163}
164
165void Preprocessor::DumpLocation(SourceLocation Loc) const {
166  Loc.dump(SourceMgr);
167}
168
169void Preprocessor::DumpMacro(const MacroInfo &MI) const {
170  llvm::errs() << "MACRO: ";
171  for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
172    DumpToken(MI.getReplacementToken(i));
173    llvm::errs() << "  ";
174  }
175  llvm::errs() << "\n";
176}
177
178void Preprocessor::PrintStats() {
179  llvm::errs() << "\n*** Preprocessor Stats:\n";
180  llvm::errs() << NumDirectives << " directives found:\n";
181  llvm::errs() << "  " << NumDefined << " #define.\n";
182  llvm::errs() << "  " << NumUndefined << " #undef.\n";
183  llvm::errs() << "  #include/#include_next/#import:\n";
184  llvm::errs() << "    " << NumEnteredSourceFiles << " source files entered.\n";
185  llvm::errs() << "    " << MaxIncludeStackDepth << " max include stack depth\n";
186  llvm::errs() << "  " << NumIf << " #if/#ifndef/#ifdef.\n";
187  llvm::errs() << "  " << NumElse << " #else/#elif.\n";
188  llvm::errs() << "  " << NumEndif << " #endif.\n";
189  llvm::errs() << "  " << NumPragma << " #pragma.\n";
190  llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
191
192  llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
193             << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
194             << NumFastMacroExpanded << " on the fast path.\n";
195  llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
196             << " token paste (##) operations performed, "
197             << NumFastTokenPaste << " on the fast path.\n";
198}
199
200Preprocessor::macro_iterator
201Preprocessor::macro_begin(bool IncludeExternalMacros) const {
202  if (IncludeExternalMacros && ExternalSource &&
203      !ReadMacrosFromExternalSource) {
204    ReadMacrosFromExternalSource = true;
205    ExternalSource->ReadDefinedMacros();
206  }
207
208  return Macros.begin();
209}
210
211Preprocessor::macro_iterator
212Preprocessor::macro_end(bool IncludeExternalMacros) const {
213  if (IncludeExternalMacros && ExternalSource &&
214      !ReadMacrosFromExternalSource) {
215    ReadMacrosFromExternalSource = true;
216    ExternalSource->ReadDefinedMacros();
217  }
218
219  return Macros.end();
220}
221
222bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
223                                          unsigned TruncateAtLine,
224                                          unsigned TruncateAtColumn) {
225  using llvm::MemoryBuffer;
226
227  CodeCompletionFile = File;
228
229  // Okay to clear out the code-completion point by passing NULL.
230  if (!CodeCompletionFile)
231    return false;
232
233  // Load the actual file's contents.
234  bool Invalid = false;
235  const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
236  if (Invalid)
237    return true;
238
239  // Find the byte position of the truncation point.
240  const char *Position = Buffer->getBufferStart();
241  for (unsigned Line = 1; Line < TruncateAtLine; ++Line) {
242    for (; *Position; ++Position) {
243      if (*Position != '\r' && *Position != '\n')
244        continue;
245
246      // Eat \r\n or \n\r as a single line.
247      if ((Position[1] == '\r' || Position[1] == '\n') &&
248          Position[0] != Position[1])
249        ++Position;
250      ++Position;
251      break;
252    }
253  }
254
255  Position += TruncateAtColumn - 1;
256
257  // Truncate the buffer.
258  if (Position < Buffer->getBufferEnd()) {
259    llvm::StringRef Data(Buffer->getBufferStart(),
260                         Position-Buffer->getBufferStart());
261    MemoryBuffer *TruncatedBuffer
262      = MemoryBuffer::getMemBufferCopy(Data, Buffer->getBufferIdentifier());
263    SourceMgr.overrideFileContents(File, TruncatedBuffer);
264  }
265
266  return false;
267}
268
269bool Preprocessor::isCodeCompletionFile(SourceLocation FileLoc) const {
270  return CodeCompletionFile && FileLoc.isFileID() &&
271    SourceMgr.getFileEntryForID(SourceMgr.getFileID(FileLoc))
272      == CodeCompletionFile;
273}
274
275void Preprocessor::CodeCompleteNaturalLanguage() {
276  SetCodeCompletionPoint(0, 0, 0);
277  getDiagnostics().setSuppressAllDiagnostics(true);
278  if (CodeComplete)
279    CodeComplete->CodeCompleteNaturalLanguage();
280}
281
282//===----------------------------------------------------------------------===//
283// Token Spelling
284//===----------------------------------------------------------------------===//
285
286/// getSpelling() - Return the 'spelling' of this token.  The spelling of a
287/// token are the characters used to represent the token in the source file
288/// after trigraph expansion and escaped-newline folding.  In particular, this
289/// wants to get the true, uncanonicalized, spelling of things like digraphs
290/// UCNs, etc.
291std::string Preprocessor::getSpelling(const Token &Tok,
292                                      const SourceManager &SourceMgr,
293                                      const LangOptions &Features,
294                                      bool *Invalid) {
295  assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
296
297  // If this token contains nothing interesting, return it directly.
298  bool CharDataInvalid = false;
299  const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation(),
300                                                    &CharDataInvalid);
301  if (Invalid)
302    *Invalid = CharDataInvalid;
303  if (CharDataInvalid)
304    return std::string();
305
306  if (!Tok.needsCleaning())
307    return std::string(TokStart, TokStart+Tok.getLength());
308
309  std::string Result;
310  Result.reserve(Tok.getLength());
311
312  // Otherwise, hard case, relex the characters into the string.
313  for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
314       Ptr != End; ) {
315    unsigned CharSize;
316    Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
317    Ptr += CharSize;
318  }
319  assert(Result.size() != unsigned(Tok.getLength()) &&
320         "NeedsCleaning flag set on something that didn't need cleaning!");
321  return Result;
322}
323
324/// getSpelling() - Return the 'spelling' of this token.  The spelling of a
325/// token are the characters used to represent the token in the source file
326/// after trigraph expansion and escaped-newline folding.  In particular, this
327/// wants to get the true, uncanonicalized, spelling of things like digraphs
328/// UCNs, etc.
329std::string Preprocessor::getSpelling(const Token &Tok, bool *Invalid) const {
330  return getSpelling(Tok, SourceMgr, Features, Invalid);
331}
332
333/// getSpelling - This method is used to get the spelling of a token into a
334/// preallocated buffer, instead of as an std::string.  The caller is required
335/// to allocate enough space for the token, which is guaranteed to be at least
336/// Tok.getLength() bytes long.  The actual length of the token is returned.
337///
338/// Note that this method may do two possible things: it may either fill in
339/// the buffer specified with characters, or it may *change the input pointer*
340/// to point to a constant buffer with the data already in it (avoiding a
341/// copy).  The caller is not allowed to modify the returned buffer pointer
342/// if an internal buffer is returned.
343unsigned Preprocessor::getSpelling(const Token &Tok,
344                                   const char *&Buffer, bool *Invalid) const {
345  assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
346
347  // If this token is an identifier, just return the string from the identifier
348  // table, which is very quick.
349  if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
350    Buffer = II->getNameStart();
351    return II->getLength();
352  }
353
354  // Otherwise, compute the start of the token in the input lexer buffer.
355  const char *TokStart = 0;
356
357  if (Tok.isLiteral())
358    TokStart = Tok.getLiteralData();
359
360  if (TokStart == 0) {
361    bool CharDataInvalid = false;
362    TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid);
363    if (Invalid)
364      *Invalid = CharDataInvalid;
365    if (CharDataInvalid) {
366      Buffer = "";
367      return 0;
368    }
369  }
370
371  // If this token contains nothing interesting, return it directly.
372  if (!Tok.needsCleaning()) {
373    Buffer = TokStart;
374    return Tok.getLength();
375  }
376
377  // Otherwise, hard case, relex the characters into the string.
378  char *OutBuf = const_cast<char*>(Buffer);
379  for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
380       Ptr != End; ) {
381    unsigned CharSize;
382    *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
383    Ptr += CharSize;
384  }
385  assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
386         "NeedsCleaning flag set on something that didn't need cleaning!");
387
388  return OutBuf-Buffer;
389}
390
391/// getSpelling - This method is used to get the spelling of a token into a
392/// SmallVector. Note that the returned StringRef may not point to the
393/// supplied buffer if a copy can be avoided.
394llvm::StringRef Preprocessor::getSpelling(const Token &Tok,
395                                          llvm::SmallVectorImpl<char> &Buffer,
396                                          bool *Invalid) const {
397  // Try the fast path.
398  if (const IdentifierInfo *II = Tok.getIdentifierInfo())
399    return II->getName();
400
401  // Resize the buffer if we need to copy into it.
402  if (Tok.needsCleaning())
403    Buffer.resize(Tok.getLength());
404
405  const char *Ptr = Buffer.data();
406  unsigned Len = getSpelling(Tok, Ptr, Invalid);
407  return llvm::StringRef(Ptr, Len);
408}
409
410/// CreateString - Plop the specified string into a scratch buffer and return a
411/// location for it.  If specified, the source location provides a source
412/// location for the token.
413void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok,
414                                SourceLocation InstantiationLoc) {
415  Tok.setLength(Len);
416
417  const char *DestPtr;
418  SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr);
419
420  if (InstantiationLoc.isValid())
421    Loc = SourceMgr.createInstantiationLoc(Loc, InstantiationLoc,
422                                           InstantiationLoc, Len);
423  Tok.setLocation(Loc);
424
425  // If this is a literal token, set the pointer data.
426  if (Tok.isLiteral())
427    Tok.setLiteralData(DestPtr);
428}
429
430
431/// AdvanceToTokenCharacter - Given a location that specifies the start of a
432/// token, return a new location that specifies a character within the token.
433SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
434                                                     unsigned CharNo) {
435  // Figure out how many physical characters away the specified instantiation
436  // character is.  This needs to take into consideration newlines and
437  // trigraphs.
438  bool Invalid = false;
439  const char *TokPtr = SourceMgr.getCharacterData(TokStart, &Invalid);
440
441  // If they request the first char of the token, we're trivially done.
442  if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr)))
443    return TokStart;
444
445  unsigned PhysOffset = 0;
446
447  // The usual case is that tokens don't contain anything interesting.  Skip
448  // over the uninteresting characters.  If a token only consists of simple
449  // chars, this method is extremely fast.
450  while (Lexer::isObviouslySimpleCharacter(*TokPtr)) {
451    if (CharNo == 0)
452      return TokStart.getFileLocWithOffset(PhysOffset);
453    ++TokPtr, --CharNo, ++PhysOffset;
454  }
455
456  // If we have a character that may be a trigraph or escaped newline, use a
457  // lexer to parse it correctly.
458  for (; CharNo; --CharNo) {
459    unsigned Size;
460    Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features);
461    TokPtr += Size;
462    PhysOffset += Size;
463  }
464
465  // Final detail: if we end up on an escaped newline, we want to return the
466  // location of the actual byte of the token.  For example foo\<newline>bar
467  // advanced by 3 should return the location of b, not of \\.  One compounding
468  // detail of this is that the escape may be made by a trigraph.
469  if (!Lexer::isObviouslySimpleCharacter(*TokPtr))
470    PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr;
471
472  return TokStart.getFileLocWithOffset(PhysOffset);
473}
474
475SourceLocation Preprocessor::getLocForEndOfToken(SourceLocation Loc,
476                                                 unsigned Offset) {
477  if (Loc.isInvalid() || !Loc.isFileID())
478    return SourceLocation();
479
480  unsigned Len = Lexer::MeasureTokenLength(Loc, getSourceManager(), Features);
481  if (Len > Offset)
482    Len = Len - Offset;
483  else
484    return Loc;
485
486  return AdvanceToTokenCharacter(Loc, Len);
487}
488
489
490
491//===----------------------------------------------------------------------===//
492// Preprocessor Initialization Methods
493//===----------------------------------------------------------------------===//
494
495
496/// EnterMainSourceFile - Enter the specified FileID as the main source file,
497/// which implicitly adds the builtin defines etc.
498void Preprocessor::EnterMainSourceFile() {
499  // We do not allow the preprocessor to reenter the main file.  Doing so will
500  // cause FileID's to accumulate information from both runs (e.g. #line
501  // information) and predefined macros aren't guaranteed to be set properly.
502  assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
503  FileID MainFileID = SourceMgr.getMainFileID();
504
505  // Enter the main file source buffer.
506  EnterSourceFile(MainFileID, 0, SourceLocation());
507
508  // If we've been asked to skip bytes in the main file (e.g., as part of a
509  // precompiled preamble), do so now.
510  if (SkipMainFilePreamble.first > 0)
511    CurLexer->SkipBytes(SkipMainFilePreamble.first,
512                        SkipMainFilePreamble.second);
513
514  // Tell the header info that the main file was entered.  If the file is later
515  // #imported, it won't be re-entered.
516  if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
517    HeaderInfo.IncrementIncludeCount(FE);
518
519  // Preprocess Predefines to populate the initial preprocessor state.
520  llvm::MemoryBuffer *SB =
521    llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
522  assert(SB && "Cannot create predefined source buffer");
523  FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
524  assert(!FID.isInvalid() && "Could not create FileID for predefines?");
525
526  // Start parsing the predefines.
527  EnterSourceFile(FID, 0, SourceLocation());
528}
529
530void Preprocessor::EndSourceFile() {
531  // Notify the client that we reached the end of the source file.
532  if (Callbacks)
533    Callbacks->EndOfMainFile();
534}
535
536//===----------------------------------------------------------------------===//
537// Lexer Event Handling.
538//===----------------------------------------------------------------------===//
539
540/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
541/// identifier information for the token and install it into the token.
542IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
543                                                   const char *BufPtr) const {
544  assert(Identifier.is(tok::identifier) && "Not an identifier!");
545  assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
546
547  // Look up this token, see if it is a macro, or if it is a language keyword.
548  IdentifierInfo *II;
549  if (BufPtr && !Identifier.needsCleaning()) {
550    // No cleaning needed, just use the characters from the lexed buffer.
551    II = getIdentifierInfo(llvm::StringRef(BufPtr, Identifier.getLength()));
552  } else {
553    // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
554    llvm::SmallString<64> IdentifierBuffer;
555    llvm::StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
556    II = getIdentifierInfo(CleanedStr);
557  }
558  Identifier.setIdentifierInfo(II);
559  return II;
560}
561
562
563/// HandleIdentifier - This callback is invoked when the lexer reads an
564/// identifier.  This callback looks up the identifier in the map and/or
565/// potentially macro expands it or turns it into a named token (like 'for').
566///
567/// Note that callers of this method are guarded by checking the
568/// IdentifierInfo's 'isHandleIdentifierCase' bit.  If this method changes, the
569/// IdentifierInfo methods that compute these properties will need to change to
570/// match.
571void Preprocessor::HandleIdentifier(Token &Identifier) {
572  assert(Identifier.getIdentifierInfo() &&
573         "Can't handle identifiers without identifier info!");
574
575  IdentifierInfo &II = *Identifier.getIdentifierInfo();
576
577  // If this identifier was poisoned, and if it was not produced from a macro
578  // expansion, emit an error.
579  if (II.isPoisoned() && CurPPLexer) {
580    if (&II != Ident__VA_ARGS__)   // We warn about __VA_ARGS__ with poisoning.
581      Diag(Identifier, diag::err_pp_used_poisoned_id);
582    else
583      Diag(Identifier, diag::ext_pp_bad_vaargs_use);
584  }
585
586  // If this is a macro to be expanded, do it.
587  if (MacroInfo *MI = getMacroInfo(&II)) {
588    if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
589      if (MI->isEnabled()) {
590        if (!HandleMacroExpandedIdentifier(Identifier, MI))
591          return;
592      } else {
593        // C99 6.10.3.4p2 says that a disabled macro may never again be
594        // expanded, even if it's in a context where it could be expanded in the
595        // future.
596        Identifier.setFlag(Token::DisableExpand);
597      }
598    }
599  }
600
601  // C++ 2.11p2: If this is an alternative representation of a C++ operator,
602  // then we act as if it is the actual operator and not the textual
603  // representation of it.
604  if (II.isCPlusPlusOperatorKeyword())
605    Identifier.setIdentifierInfo(0);
606
607  // If this is an extension token, diagnose its use.
608  // We avoid diagnosing tokens that originate from macro definitions.
609  // FIXME: This warning is disabled in cases where it shouldn't be,
610  // like "#define TY typeof", "TY(1) x".
611  if (II.isExtensionToken() && !DisableMacroExpansion)
612    Diag(Identifier, diag::ext_token_used);
613}
614
615void Preprocessor::AddCommentHandler(CommentHandler *Handler) {
616  assert(Handler && "NULL comment handler");
617  assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) ==
618         CommentHandlers.end() && "Comment handler already registered");
619  CommentHandlers.push_back(Handler);
620}
621
622void Preprocessor::RemoveCommentHandler(CommentHandler *Handler) {
623  std::vector<CommentHandler *>::iterator Pos
624  = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler);
625  assert(Pos != CommentHandlers.end() && "Comment handler not registered");
626  CommentHandlers.erase(Pos);
627}
628
629bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
630  bool AnyPendingTokens = false;
631  for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
632       HEnd = CommentHandlers.end();
633       H != HEnd; ++H) {
634    if ((*H)->HandleComment(*this, Comment))
635      AnyPendingTokens = true;
636  }
637  if (!AnyPendingTokens || getCommentRetentionState())
638    return false;
639  Lex(result);
640  return true;
641}
642
643CommentHandler::~CommentHandler() { }
644
645CodeCompletionHandler::~CodeCompletionHandler() { }
646
647void Preprocessor::createPreprocessingRecord() {
648  if (Record)
649    return;
650
651  Record = new PreprocessingRecord;
652  addPPCallbacks(Record);
653}
654