Preprocessor.cpp revision e93433db8bc4e6a8e8f0b1b55728085ce0f276a9
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/Lex/ModuleLoader.h"
39#include "clang/Basic/SourceManager.h"
40#include "clang/Basic/FileManager.h"
41#include "clang/Basic/TargetInfo.h"
42#include "llvm/ADT/APFloat.h"
43#include "llvm/ADT/SmallVector.h"
44#include "llvm/Support/MemoryBuffer.h"
45#include "llvm/Support/raw_ostream.h"
46#include "llvm/Support/Capacity.h"
47using namespace clang;
48
49//===----------------------------------------------------------------------===//
50ExternalPreprocessorSource::~ExternalPreprocessorSource() { }
51
52Preprocessor::Preprocessor(DiagnosticsEngine &diags, LangOptions &opts,
53                           const TargetInfo *target, SourceManager &SM,
54                           HeaderSearch &Headers, ModuleLoader &TheModuleLoader,
55                           IdentifierInfoLookup* IILookup,
56                           bool OwnsHeaders,
57                           bool DelayInitialization)
58  : Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()),
59    SourceMgr(SM), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader),
60    ExternalSource(0),
61    Identifiers(opts, IILookup), CodeComplete(0),
62    CodeCompletionFile(0), CodeCompletionOffset(0), CodeCompletionReached(0),
63    SkipMainFilePreamble(0, true), CurPPLexer(0),
64    CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), MacroArgCache(0),
65    Record(0), MIChainHead(0), MICache(0)
66{
67  OwnsHeaderSearch = OwnsHeaders;
68
69  if (!DelayInitialization) {
70    assert(Target && "Must provide target information for PP initialization");
71    Initialize(*Target);
72  }
73}
74
75Preprocessor::~Preprocessor() {
76  assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
77
78  while (!IncludeMacroStack.empty()) {
79    delete IncludeMacroStack.back().TheLexer;
80    delete IncludeMacroStack.back().TheTokenLexer;
81    IncludeMacroStack.pop_back();
82  }
83
84  // Free any macro definitions.
85  for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next)
86    I->MI.Destroy();
87
88  // Free any cached macro expanders.
89  for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
90    delete TokenLexerCache[i];
91
92  // Free any cached MacroArgs.
93  for (MacroArgs *ArgList = MacroArgCache; ArgList; )
94    ArgList = ArgList->deallocate();
95
96  // Release pragma information.
97  delete PragmaHandlers;
98
99  // Delete the scratch buffer info.
100  delete ScratchBuf;
101
102  // Delete the header search info, if we own it.
103  if (OwnsHeaderSearch)
104    delete &HeaderInfo;
105
106  delete Callbacks;
107}
108
109void Preprocessor::Initialize(const TargetInfo &Target) {
110  assert((!this->Target || this->Target == &Target) &&
111         "Invalid override of target information");
112  this->Target = &Target;
113
114  // Initialize information about built-ins.
115  BuiltinInfo.InitializeTarget(Target);
116
117  ScratchBuf = new ScratchBuffer(SourceMgr);
118  CounterValue = 0; // __COUNTER__ starts at 0.
119
120  // Clear stats.
121  NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
122  NumIf = NumElse = NumEndif = 0;
123  NumEnteredSourceFiles = 0;
124  NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
125  NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
126  MaxIncludeStackDepth = 0;
127  NumSkipped = 0;
128
129  // Default to discarding comments.
130  KeepComments = false;
131  KeepMacroComments = false;
132  SuppressIncludeNotFoundError = false;
133  AutoModuleImport = false;
134
135  // Macro expansion is enabled.
136  DisableMacroExpansion = false;
137  InMacroArgs = false;
138  NumCachedTokenLexers = 0;
139
140  CachedLexPos = 0;
141
142  // We haven't read anything from the external source.
143  ReadMacrosFromExternalSource = false;
144
145  // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
146  // This gets unpoisoned where it is allowed.
147  (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
148  SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use);
149
150  // Initialize the pragma handlers.
151  PragmaHandlers = new PragmaNamespace(StringRef());
152  RegisterBuiltinPragmas();
153
154  // Initialize builtin macros like __LINE__ and friends.
155  RegisterBuiltinMacros();
156
157  if(Features.Borland) {
158    Ident__exception_info        = getIdentifierInfo("_exception_info");
159    Ident___exception_info       = getIdentifierInfo("__exception_info");
160    Ident_GetExceptionInfo       = getIdentifierInfo("GetExceptionInformation");
161    Ident__exception_code        = getIdentifierInfo("_exception_code");
162    Ident___exception_code       = getIdentifierInfo("__exception_code");
163    Ident_GetExceptionCode       = getIdentifierInfo("GetExceptionCode");
164    Ident__abnormal_termination  = getIdentifierInfo("_abnormal_termination");
165    Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination");
166    Ident_AbnormalTermination    = getIdentifierInfo("AbnormalTermination");
167  } else {
168    Ident__exception_info = Ident__exception_code = Ident__abnormal_termination = 0;
169    Ident___exception_info = Ident___exception_code = Ident___abnormal_termination = 0;
170    Ident_GetExceptionInfo = Ident_GetExceptionCode = Ident_AbnormalTermination = 0;
171  }
172}
173
174void Preprocessor::setPTHManager(PTHManager* pm) {
175  PTH.reset(pm);
176  FileMgr.addStatCache(PTH->createStatCache());
177}
178
179void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
180  llvm::errs() << tok::getTokenName(Tok.getKind()) << " '"
181               << getSpelling(Tok) << "'";
182
183  if (!DumpFlags) return;
184
185  llvm::errs() << "\t";
186  if (Tok.isAtStartOfLine())
187    llvm::errs() << " [StartOfLine]";
188  if (Tok.hasLeadingSpace())
189    llvm::errs() << " [LeadingSpace]";
190  if (Tok.isExpandDisabled())
191    llvm::errs() << " [ExpandDisabled]";
192  if (Tok.needsCleaning()) {
193    const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
194    llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength())
195                 << "']";
196  }
197
198  llvm::errs() << "\tLoc=<";
199  DumpLocation(Tok.getLocation());
200  llvm::errs() << ">";
201}
202
203void Preprocessor::DumpLocation(SourceLocation Loc) const {
204  Loc.dump(SourceMgr);
205}
206
207void Preprocessor::DumpMacro(const MacroInfo &MI) const {
208  llvm::errs() << "MACRO: ";
209  for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
210    DumpToken(MI.getReplacementToken(i));
211    llvm::errs() << "  ";
212  }
213  llvm::errs() << "\n";
214}
215
216void Preprocessor::PrintStats() {
217  llvm::errs() << "\n*** Preprocessor Stats:\n";
218  llvm::errs() << NumDirectives << " directives found:\n";
219  llvm::errs() << "  " << NumDefined << " #define.\n";
220  llvm::errs() << "  " << NumUndefined << " #undef.\n";
221  llvm::errs() << "  #include/#include_next/#import:\n";
222  llvm::errs() << "    " << NumEnteredSourceFiles << " source files entered.\n";
223  llvm::errs() << "    " << MaxIncludeStackDepth << " max include stack depth\n";
224  llvm::errs() << "  " << NumIf << " #if/#ifndef/#ifdef.\n";
225  llvm::errs() << "  " << NumElse << " #else/#elif.\n";
226  llvm::errs() << "  " << NumEndif << " #endif.\n";
227  llvm::errs() << "  " << NumPragma << " #pragma.\n";
228  llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
229
230  llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
231             << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
232             << NumFastMacroExpanded << " on the fast path.\n";
233  llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
234             << " token paste (##) operations performed, "
235             << NumFastTokenPaste << " on the fast path.\n";
236}
237
238Preprocessor::macro_iterator
239Preprocessor::macro_begin(bool IncludeExternalMacros) const {
240  if (IncludeExternalMacros && ExternalSource &&
241      !ReadMacrosFromExternalSource) {
242    ReadMacrosFromExternalSource = true;
243    ExternalSource->ReadDefinedMacros();
244  }
245
246  return Macros.begin();
247}
248
249size_t Preprocessor::getTotalMemory() const {
250  return BP.getTotalMemory()
251    + llvm::capacity_in_bytes(MacroExpandedTokens)
252    + Predefines.capacity() /* Predefines buffer. */
253    + llvm::capacity_in_bytes(Macros)
254    + llvm::capacity_in_bytes(PragmaPushMacroInfo)
255    + llvm::capacity_in_bytes(PoisonReasons)
256    + llvm::capacity_in_bytes(CommentHandlers);
257}
258
259Preprocessor::macro_iterator
260Preprocessor::macro_end(bool IncludeExternalMacros) const {
261  if (IncludeExternalMacros && ExternalSource &&
262      !ReadMacrosFromExternalSource) {
263    ReadMacrosFromExternalSource = true;
264    ExternalSource->ReadDefinedMacros();
265  }
266
267  return Macros.end();
268}
269
270bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
271                                          unsigned CompleteLine,
272                                          unsigned CompleteColumn) {
273  assert(File);
274  assert(CompleteLine && CompleteColumn && "Starts from 1:1");
275  assert(!CodeCompletionFile && "Already set");
276
277  using llvm::MemoryBuffer;
278
279  // Load the actual file's contents.
280  bool Invalid = false;
281  const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
282  if (Invalid)
283    return true;
284
285  // Find the byte position of the truncation point.
286  const char *Position = Buffer->getBufferStart();
287  for (unsigned Line = 1; Line < CompleteLine; ++Line) {
288    for (; *Position; ++Position) {
289      if (*Position != '\r' && *Position != '\n')
290        continue;
291
292      // Eat \r\n or \n\r as a single line.
293      if ((Position[1] == '\r' || Position[1] == '\n') &&
294          Position[0] != Position[1])
295        ++Position;
296      ++Position;
297      break;
298    }
299  }
300
301  Position += CompleteColumn - 1;
302
303  // Insert '\0' at the code-completion point.
304  if (Position < Buffer->getBufferEnd()) {
305    CodeCompletionFile = File;
306    CodeCompletionOffset = Position - Buffer->getBufferStart();
307
308    MemoryBuffer *NewBuffer =
309        MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1,
310                                            Buffer->getBufferIdentifier());
311    char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart());
312    char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
313    *NewPos = '\0';
314    std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
315    SourceMgr.overrideFileContents(File, NewBuffer);
316  }
317
318  return false;
319}
320
321void Preprocessor::CodeCompleteNaturalLanguage() {
322  if (CodeComplete)
323    CodeComplete->CodeCompleteNaturalLanguage();
324  setCodeCompletionReached();
325}
326
327/// getSpelling - This method is used to get the spelling of a token into a
328/// SmallVector. Note that the returned StringRef may not point to the
329/// supplied buffer if a copy can be avoided.
330StringRef Preprocessor::getSpelling(const Token &Tok,
331                                          SmallVectorImpl<char> &Buffer,
332                                          bool *Invalid) const {
333  // NOTE: this has to be checked *before* testing for an IdentifierInfo.
334  if (Tok.isNot(tok::raw_identifier)) {
335    // Try the fast path.
336    if (const IdentifierInfo *II = Tok.getIdentifierInfo())
337      return II->getName();
338  }
339
340  // Resize the buffer if we need to copy into it.
341  if (Tok.needsCleaning())
342    Buffer.resize(Tok.getLength());
343
344  const char *Ptr = Buffer.data();
345  unsigned Len = getSpelling(Tok, Ptr, Invalid);
346  return StringRef(Ptr, Len);
347}
348
349/// CreateString - Plop the specified string into a scratch buffer and return a
350/// location for it.  If specified, the source location provides a source
351/// location for the token.
352void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok,
353                                SourceLocation ExpansionLocStart,
354                                SourceLocation ExpansionLocEnd) {
355  Tok.setLength(Len);
356
357  const char *DestPtr;
358  SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr);
359
360  if (ExpansionLocStart.isValid())
361    Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart,
362                                       ExpansionLocEnd, Len);
363  Tok.setLocation(Loc);
364
365  // If this is a raw identifier or a literal token, set the pointer data.
366  if (Tok.is(tok::raw_identifier))
367    Tok.setRawIdentifierData(DestPtr);
368  else if (Tok.isLiteral())
369    Tok.setLiteralData(DestPtr);
370}
371
372
373
374//===----------------------------------------------------------------------===//
375// Preprocessor Initialization Methods
376//===----------------------------------------------------------------------===//
377
378
379/// EnterMainSourceFile - Enter the specified FileID as the main source file,
380/// which implicitly adds the builtin defines etc.
381void Preprocessor::EnterMainSourceFile() {
382  // We do not allow the preprocessor to reenter the main file.  Doing so will
383  // cause FileID's to accumulate information from both runs (e.g. #line
384  // information) and predefined macros aren't guaranteed to be set properly.
385  assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
386  FileID MainFileID = SourceMgr.getMainFileID();
387
388  // Enter the main file source buffer.
389  EnterSourceFile(MainFileID, 0, SourceLocation());
390
391  // If we've been asked to skip bytes in the main file (e.g., as part of a
392  // precompiled preamble), do so now.
393  if (SkipMainFilePreamble.first > 0)
394    CurLexer->SkipBytes(SkipMainFilePreamble.first,
395                        SkipMainFilePreamble.second);
396
397  // Tell the header info that the main file was entered.  If the file is later
398  // #imported, it won't be re-entered.
399  if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
400    HeaderInfo.IncrementIncludeCount(FE);
401
402  // Preprocess Predefines to populate the initial preprocessor state.
403  llvm::MemoryBuffer *SB =
404    llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
405  assert(SB && "Cannot create predefined source buffer");
406  FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
407  assert(!FID.isInvalid() && "Could not create FileID for predefines?");
408
409  // Start parsing the predefines.
410  EnterSourceFile(FID, 0, SourceLocation());
411}
412
413void Preprocessor::EndSourceFile() {
414  // Notify the client that we reached the end of the source file.
415  if (Callbacks)
416    Callbacks->EndOfMainFile();
417}
418
419//===----------------------------------------------------------------------===//
420// Lexer Event Handling.
421//===----------------------------------------------------------------------===//
422
423/// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
424/// identifier information for the token and install it into the token,
425/// updating the token kind accordingly.
426IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
427  assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!");
428
429  // Look up this token, see if it is a macro, or if it is a language keyword.
430  IdentifierInfo *II;
431  if (!Identifier.needsCleaning()) {
432    // No cleaning needed, just use the characters from the lexed buffer.
433    II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(),
434                                           Identifier.getLength()));
435  } else {
436    // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
437    llvm::SmallString<64> IdentifierBuffer;
438    StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
439    II = getIdentifierInfo(CleanedStr);
440  }
441
442  // Update the token info (identifier info and appropriate token kind).
443  Identifier.setIdentifierInfo(II);
444  Identifier.setKind(II->getTokenID());
445
446  return II;
447}
448
449void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) {
450  PoisonReasons[II] = DiagID;
451}
452
453void Preprocessor::PoisonSEHIdentifiers(bool Poison) {
454  assert(Ident__exception_code && Ident__exception_info);
455  assert(Ident___exception_code && Ident___exception_info);
456  Ident__exception_code->setIsPoisoned(Poison);
457  Ident___exception_code->setIsPoisoned(Poison);
458  Ident_GetExceptionCode->setIsPoisoned(Poison);
459  Ident__exception_info->setIsPoisoned(Poison);
460  Ident___exception_info->setIsPoisoned(Poison);
461  Ident_GetExceptionInfo->setIsPoisoned(Poison);
462  Ident__abnormal_termination->setIsPoisoned(Poison);
463  Ident___abnormal_termination->setIsPoisoned(Poison);
464  Ident_AbnormalTermination->setIsPoisoned(Poison);
465}
466
467void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) {
468  assert(Identifier.getIdentifierInfo() &&
469         "Can't handle identifiers without identifier info!");
470  llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it =
471    PoisonReasons.find(Identifier.getIdentifierInfo());
472  if(it == PoisonReasons.end())
473    Diag(Identifier, diag::err_pp_used_poisoned_id);
474  else
475    Diag(Identifier,it->second) << Identifier.getIdentifierInfo();
476}
477
478/// HandleIdentifier - This callback is invoked when the lexer reads an
479/// identifier.  This callback looks up the identifier in the map and/or
480/// potentially macro expands it or turns it into a named token (like 'for').
481///
482/// Note that callers of this method are guarded by checking the
483/// IdentifierInfo's 'isHandleIdentifierCase' bit.  If this method changes, the
484/// IdentifierInfo methods that compute these properties will need to change to
485/// match.
486void Preprocessor::HandleIdentifier(Token &Identifier) {
487  assert(Identifier.getIdentifierInfo() &&
488         "Can't handle identifiers without identifier info!");
489
490  IdentifierInfo &II = *Identifier.getIdentifierInfo();
491
492  // If the information about this identifier is out of date, update it from
493  // the external source.
494  if (II.isOutOfDate()) {
495    ExternalSource->updateOutOfDateIdentifier(II);
496    Identifier.setKind(II.getTokenID());
497  }
498
499  // If this identifier was poisoned, and if it was not produced from a macro
500  // expansion, emit an error.
501  if (II.isPoisoned() && CurPPLexer) {
502    HandlePoisonedIdentifier(Identifier);
503  }
504
505  // If this is a macro to be expanded, do it.
506  if (MacroInfo *MI = getMacroInfo(&II)) {
507    if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
508      if (MI->isEnabled()) {
509        if (!HandleMacroExpandedIdentifier(Identifier, MI))
510          return;
511      } else {
512        // C99 6.10.3.4p2 says that a disabled macro may never again be
513        // expanded, even if it's in a context where it could be expanded in the
514        // future.
515        Identifier.setFlag(Token::DisableExpand);
516      }
517    }
518  }
519
520  // If this identifier is a keyword in C++11, produce a warning. Don't warn if
521  // we're not considering macro expansion, since this identifier might be the
522  // name of a macro.
523  // FIXME: This warning is disabled in cases where it shouldn't be, like
524  //   "#define constexpr constexpr", "int constexpr;"
525  if (II.isCXX11CompatKeyword() & !DisableMacroExpansion) {
526    Diag(Identifier, diag::warn_cxx11_keyword) << II.getName();
527    // Don't diagnose this keyword again in this translation unit.
528    II.setIsCXX11CompatKeyword(false);
529  }
530
531  // C++ 2.11p2: If this is an alternative representation of a C++ operator,
532  // then we act as if it is the actual operator and not the textual
533  // representation of it.
534  if (II.isCPlusPlusOperatorKeyword())
535    Identifier.setIdentifierInfo(0);
536
537  // If this is an extension token, diagnose its use.
538  // We avoid diagnosing tokens that originate from macro definitions.
539  // FIXME: This warning is disabled in cases where it shouldn't be,
540  // like "#define TY typeof", "TY(1) x".
541  if (II.isExtensionToken() && !DisableMacroExpansion)
542    Diag(Identifier, diag::ext_token_used);
543
544  // If this is the '__import_module__' keyword, note that the next token
545  // indicates a module name.
546  if (II.getTokenID() == tok::kw___import_module__ &&
547      !InMacroArgs && !DisableMacroExpansion) {
548    ModuleImportLoc = Identifier.getLocation();
549    CurLexerKind = CLK_LexAfterModuleImport;
550  }
551}
552
553/// \brief Lex a token following the __import_module__ keyword.
554void Preprocessor::LexAfterModuleImport(Token &Result) {
555  // Figure out what kind of lexer we actually have.
556  if (CurLexer)
557    CurLexerKind = CLK_Lexer;
558  else if (CurPTHLexer)
559    CurLexerKind = CLK_PTHLexer;
560  else if (CurTokenLexer)
561    CurLexerKind = CLK_TokenLexer;
562  else
563    CurLexerKind = CLK_CachingLexer;
564
565  // Lex the next token.
566  Lex(Result);
567
568  // The token sequence
569  //
570  //   __import_module__ identifier
571  //
572  // indicates a module import directive. We already saw the __import_module__
573  // keyword, so now we're looking for the identifier.
574  if (Result.getKind() != tok::identifier)
575    return;
576
577  // Load the module.
578  (void)TheModuleLoader.loadModule(ModuleImportLoc,
579                                   *Result.getIdentifierInfo(),
580                                   Result.getLocation());
581}
582
583void Preprocessor::AddCommentHandler(CommentHandler *Handler) {
584  assert(Handler && "NULL comment handler");
585  assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) ==
586         CommentHandlers.end() && "Comment handler already registered");
587  CommentHandlers.push_back(Handler);
588}
589
590void Preprocessor::RemoveCommentHandler(CommentHandler *Handler) {
591  std::vector<CommentHandler *>::iterator Pos
592  = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler);
593  assert(Pos != CommentHandlers.end() && "Comment handler not registered");
594  CommentHandlers.erase(Pos);
595}
596
597bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
598  bool AnyPendingTokens = false;
599  for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
600       HEnd = CommentHandlers.end();
601       H != HEnd; ++H) {
602    if ((*H)->HandleComment(*this, Comment))
603      AnyPendingTokens = true;
604  }
605  if (!AnyPendingTokens || getCommentRetentionState())
606    return false;
607  Lex(result);
608  return true;
609}
610
611ModuleLoader::~ModuleLoader() { }
612
613CommentHandler::~CommentHandler() { }
614
615CodeCompletionHandler::~CodeCompletionHandler() { }
616
617void Preprocessor::createPreprocessingRecord(
618                                      bool IncludeNestedMacroExpansions) {
619  if (Record)
620    return;
621
622  Record = new PreprocessingRecord(getSourceManager(),
623                                   IncludeNestedMacroExpansions);
624  addPPCallbacks(Record);
625}
626