blob: 3b070ce049db704a8b9549eb2dedc5e70e78cd46 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13//
Chris Lattner22eb9722006-06-18 05:43:12 +000014// Options to support:
15// -H - Print the name of each header file used.
Chris Lattner1630c3c2009-02-06 06:45:26 +000016// -d[DNI] - Dump various things.
Chris Lattner22eb9722006-06-18 05:43:12 +000017// -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//
Chris Lattner22eb9722006-06-18 05:43:12 +000026//===----------------------------------------------------------------------===//
27
28#include "clang/Lex/Preprocessor.h"
Douglas Gregor1452ff12012-10-24 17:46:57 +000029#include "clang/Lex/PreprocessorOptions.h"
Chris Lattnerd19564b2009-12-15 01:51:03 +000030#include "MacroArgs.h"
Douglas Gregor9882a5a2010-01-04 19:18:44 +000031#include "clang/Lex/ExternalPreprocessorSource.h"
Chris Lattner07b019a2006-10-22 07:28:56 +000032#include "clang/Lex/HeaderSearch.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000033#include "clang/Lex/MacroInfo.h"
Chris Lattnerb8761832006-06-24 21:31:03 +000034#include "clang/Lex/Pragma.h"
Douglas Gregor7f6d60d2010-03-19 16:15:56 +000035#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner0b8cfc22006-06-28 06:49:17 +000036#include "clang/Lex/ScratchBuffer.h"
Chris Lattner60f36222009-01-29 05:15:15 +000037#include "clang/Lex/LexDiagnostic.h"
Douglas Gregor3a7ad252010-08-24 19:08:16 +000038#include "clang/Lex/CodeCompletionHandler.h"
Douglas Gregor08142532011-08-26 23:56:07 +000039#include "clang/Lex/ModuleLoader.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000040#include "clang/Basic/SourceManager.h"
Ted Kremeneka5c2c272009-02-12 03:26:59 +000041#include "clang/Basic/FileManager.h"
Chris Lattner81278c62006-10-14 19:03:49 +000042#include "clang/Basic/TargetInfo.h"
Chris Lattner5cd83512008-10-05 20:40:30 +000043#include "llvm/ADT/APFloat.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000044#include "llvm/ADT/SmallString.h"
Chris Lattner8a7003c2007-07-16 06:48:38 +000045#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000046#include "llvm/Support/raw_ostream.h"
Ted Kremenek8b77fe72011-07-27 18:41:23 +000047#include "llvm/Support/Capacity.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000048using namespace clang;
49
50//===----------------------------------------------------------------------===//
Douglas Gregor9882a5a2010-01-04 19:18:44 +000051ExternalPreprocessorSource::~ExternalPreprocessorSource() { }
Chris Lattner22eb9722006-06-18 05:43:12 +000052
Douglas Gregorcb28f9d2012-10-09 23:05:51 +000053PPMutationListener::~PPMutationListener() { }
54
Douglas Gregor1452ff12012-10-24 17:46:57 +000055Preprocessor::Preprocessor(llvm::IntrusiveRefCntPtr<PreprocessorOptions> PPOpts,
56 DiagnosticsEngine &diags, LangOptions &opts,
Douglas Gregor83297df2011-09-01 23:39:15 +000057 const TargetInfo *target, SourceManager &SM,
Douglas Gregor08142532011-08-26 23:56:07 +000058 HeaderSearch &Headers, ModuleLoader &TheModuleLoader,
Daniel Dunbar0c6c9302009-11-11 21:44:21 +000059 IdentifierInfoLookup* IILookup,
Douglas Gregor83297df2011-09-01 23:39:15 +000060 bool OwnsHeaders,
Axel Naumann2eb1d902012-03-16 10:40:17 +000061 bool DelayInitialization,
62 bool IncrProcessing)
Douglas Gregor1452ff12012-10-24 17:46:57 +000063 : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), Target(target),
64 FileMgr(Headers.getFileMgr()),
Douglas Gregor08142532011-08-26 23:56:07 +000065 SourceMgr(SM), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader),
Axel Naumann2eb1d902012-03-16 10:40:17 +000066 ExternalSource(0), Identifiers(opts, IILookup),
67 IncrementalProcessing(IncrProcessing), CodeComplete(0),
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000068 CodeCompletionFile(0), CodeCompletionOffset(0), CodeCompletionReached(0),
69 SkipMainFilePreamble(0, true), CurPPLexer(0),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +000070 CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), Listener(0),
71 MacroArgCache(0), Record(0), MIChainHead(0), MICache(0)
Douglas Gregor83297df2011-09-01 23:39:15 +000072{
Daniel Dunbar0c6c9302009-11-11 21:44:21 +000073 OwnsHeaderSearch = OwnsHeaders;
Douglas Gregor83297df2011-09-01 23:39:15 +000074
75 ScratchBuf = new ScratchBuffer(SourceMgr);
76 CounterValue = 0; // __COUNTER__ starts at 0.
77
78 // Clear stats.
79 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
80 NumIf = NumElse = NumEndif = 0;
81 NumEnteredSourceFiles = 0;
82 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
83 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
84 MaxIncludeStackDepth = 0;
85 NumSkipped = 0;
86
87 // Default to discarding comments.
88 KeepComments = false;
89 KeepMacroComments = false;
90 SuppressIncludeNotFoundError = false;
91
92 // Macro expansion is enabled.
93 DisableMacroExpansion = false;
David Blaikied5321242012-06-06 18:52:13 +000094 MacroExpansionInDirectivesOverride = false;
Douglas Gregor83297df2011-09-01 23:39:15 +000095 InMacroArgs = false;
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +000096 InMacroArgPreExpansion = false;
Douglas Gregor83297df2011-09-01 23:39:15 +000097 NumCachedTokenLexers = 0;
Jordan Rosede1a2922012-06-08 18:06:21 +000098 PragmasEnabled = true;
99
Douglas Gregor83297df2011-09-01 23:39:15 +0000100 CachedLexPos = 0;
101
102 // We haven't read anything from the external source.
103 ReadMacrosFromExternalSource = false;
104
Douglas Gregor83297df2011-09-01 23:39:15 +0000105 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
106 // This gets unpoisoned where it is allowed.
107 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
108 SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use);
109
110 // Initialize the pragma handlers.
111 PragmaHandlers = new PragmaNamespace(StringRef());
112 RegisterBuiltinPragmas();
113
114 // Initialize builtin macros like __LINE__ and friends.
115 RegisterBuiltinMacros();
116
David Blaikiebbafb8a2012-03-11 07:00:24 +0000117 if(LangOpts.Borland) {
Douglas Gregor83297df2011-09-01 23:39:15 +0000118 Ident__exception_info = getIdentifierInfo("_exception_info");
119 Ident___exception_info = getIdentifierInfo("__exception_info");
120 Ident_GetExceptionInfo = getIdentifierInfo("GetExceptionInformation");
121 Ident__exception_code = getIdentifierInfo("_exception_code");
122 Ident___exception_code = getIdentifierInfo("__exception_code");
123 Ident_GetExceptionCode = getIdentifierInfo("GetExceptionCode");
124 Ident__abnormal_termination = getIdentifierInfo("_abnormal_termination");
125 Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination");
126 Ident_AbnormalTermination = getIdentifierInfo("AbnormalTermination");
127 } else {
128 Ident__exception_info = Ident__exception_code = Ident__abnormal_termination = 0;
129 Ident___exception_info = Ident___exception_code = Ident___abnormal_termination = 0;
130 Ident_GetExceptionInfo = Ident_GetExceptionCode = Ident_AbnormalTermination = 0;
Douglas Gregor89929282012-01-30 06:01:29 +0000131 }
Argyrios Kyrtzidis3c9aaf12012-06-02 18:08:09 +0000132
133 if (!DelayInitialization) {
134 assert(Target && "Must provide target information for PP initialization");
135 Initialize(*Target);
136 }
137}
138
139Preprocessor::~Preprocessor() {
140 assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
141
142 while (!IncludeMacroStack.empty()) {
143 delete IncludeMacroStack.back().TheLexer;
144 delete IncludeMacroStack.back().TheTokenLexer;
145 IncludeMacroStack.pop_back();
146 }
147
148 // Free any macro definitions.
149 for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next)
150 I->MI.Destroy();
151
152 // Free any cached macro expanders.
153 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
154 delete TokenLexerCache[i];
155
156 // Free any cached MacroArgs.
157 for (MacroArgs *ArgList = MacroArgCache; ArgList; )
158 ArgList = ArgList->deallocate();
159
160 // Release pragma information.
161 delete PragmaHandlers;
162
163 // Delete the scratch buffer info.
164 delete ScratchBuf;
165
166 // Delete the header search info, if we own it.
167 if (OwnsHeaderSearch)
168 delete &HeaderInfo;
169
170 delete Callbacks;
171}
172
173void Preprocessor::Initialize(const TargetInfo &Target) {
174 assert((!this->Target || this->Target == &Target) &&
175 "Invalid override of target information");
176 this->Target = &Target;
Douglas Gregor89929282012-01-30 06:01:29 +0000177
Argyrios Kyrtzidis3c9aaf12012-06-02 18:08:09 +0000178 // Initialize information about built-ins.
179 BuiltinInfo.InitializeTarget(Target);
Douglas Gregor89929282012-01-30 06:01:29 +0000180 HeaderInfo.setTarget(Target);
Douglas Gregor83297df2011-09-01 23:39:15 +0000181}
182
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000183void Preprocessor::setPTHManager(PTHManager* pm) {
184 PTH.reset(pm);
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000185 FileMgr.addStatCache(PTH->createStatCache());
Ted Kremeneka5c2c272009-02-12 03:26:59 +0000186}
187
Chris Lattner146762e2007-07-20 16:59:19 +0000188void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000189 llvm::errs() << tok::getTokenName(Tok.getKind()) << " '"
190 << getSpelling(Tok) << "'";
Mike Stump11289f42009-09-09 15:08:12 +0000191
Chris Lattnerd01e2912006-06-18 16:22:51 +0000192 if (!DumpFlags) return;
Mike Stump11289f42009-09-09 15:08:12 +0000193
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000194 llvm::errs() << "\t";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000195 if (Tok.isAtStartOfLine())
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000196 llvm::errs() << " [StartOfLine]";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000197 if (Tok.hasLeadingSpace())
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000198 llvm::errs() << " [LeadingSpace]";
Chris Lattner6e4bf522006-07-27 06:59:25 +0000199 if (Tok.isExpandDisabled())
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000200 llvm::errs() << " [ExpandDisabled]";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000201 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000202 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000203 llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength())
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000204 << "']";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000205 }
Mike Stump11289f42009-09-09 15:08:12 +0000206
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000207 llvm::errs() << "\tLoc=<";
Chris Lattner615315f2007-12-09 20:31:55 +0000208 DumpLocation(Tok.getLocation());
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000209 llvm::errs() << ">";
Chris Lattner615315f2007-12-09 20:31:55 +0000210}
211
212void Preprocessor::DumpLocation(SourceLocation Loc) const {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000213 Loc.dump(SourceMgr);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000214}
215
216void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000217 llvm::errs() << "MACRO: ";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000218 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
219 DumpToken(MI.getReplacementToken(i));
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000220 llvm::errs() << " ";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000221 }
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000222 llvm::errs() << "\n";
Chris Lattnerd01e2912006-06-18 16:22:51 +0000223}
224
Chris Lattner22eb9722006-06-18 05:43:12 +0000225void Preprocessor::PrintStats() {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000226 llvm::errs() << "\n*** Preprocessor Stats:\n";
227 llvm::errs() << NumDirectives << " directives found:\n";
228 llvm::errs() << " " << NumDefined << " #define.\n";
229 llvm::errs() << " " << NumUndefined << " #undef.\n";
230 llvm::errs() << " #include/#include_next/#import:\n";
231 llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n";
232 llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n";
233 llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n";
234 llvm::errs() << " " << NumElse << " #else/#elif.\n";
235 llvm::errs() << " " << NumEndif << " #endif.\n";
236 llvm::errs() << " " << NumPragma << " #pragma.\n";
237 llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000238
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000239 llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000240 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
241 << NumFastMacroExpanded << " on the fast path.\n";
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000242 llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
Ted Kremeneka0a3e9b2008-01-14 16:44:48 +0000243 << " token paste (##) operations performed, "
244 << NumFastTokenPaste << " on the fast path.\n";
Alexander Kornienko199cd942012-08-13 10:46:42 +0000245
246 llvm::errs() << "\nPreprocessor Memory: " << getTotalMemory() << "B total";
247
248 llvm::errs() << "\n BumpPtr: " << BP.getTotalMemory();
249 llvm::errs() << "\n Macro Expanded Tokens: "
250 << llvm::capacity_in_bytes(MacroExpandedTokens);
251 llvm::errs() << "\n Predefines Buffer: " << Predefines.capacity();
252 llvm::errs() << "\n Macros: " << llvm::capacity_in_bytes(Macros);
253 llvm::errs() << "\n #pragma push_macro Info: "
254 << llvm::capacity_in_bytes(PragmaPushMacroInfo);
255 llvm::errs() << "\n Poison Reasons: "
256 << llvm::capacity_in_bytes(PoisonReasons);
257 llvm::errs() << "\n Comment Handlers: "
258 << llvm::capacity_in_bytes(CommentHandlers) << "\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000259}
260
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000261Preprocessor::macro_iterator
262Preprocessor::macro_begin(bool IncludeExternalMacros) const {
263 if (IncludeExternalMacros && ExternalSource &&
Douglas Gregor9882a5a2010-01-04 19:18:44 +0000264 !ReadMacrosFromExternalSource) {
265 ReadMacrosFromExternalSource = true;
266 ExternalSource->ReadDefinedMacros();
267 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000268
269 return Macros.begin();
Douglas Gregor9882a5a2010-01-04 19:18:44 +0000270}
271
Argyrios Kyrtzidise379ee32011-06-29 22:20:04 +0000272size_t Preprocessor::getTotalMemory() const {
Ted Kremenek182543a2011-07-26 21:17:24 +0000273 return BP.getTotalMemory()
Ted Kremenek8b77fe72011-07-27 18:41:23 +0000274 + llvm::capacity_in_bytes(MacroExpandedTokens)
Ted Kremenek182543a2011-07-26 21:17:24 +0000275 + Predefines.capacity() /* Predefines buffer. */
Ted Kremenek8b77fe72011-07-27 18:41:23 +0000276 + llvm::capacity_in_bytes(Macros)
277 + llvm::capacity_in_bytes(PragmaPushMacroInfo)
278 + llvm::capacity_in_bytes(PoisonReasons)
279 + llvm::capacity_in_bytes(CommentHandlers);
Argyrios Kyrtzidise379ee32011-06-29 22:20:04 +0000280}
281
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000282Preprocessor::macro_iterator
283Preprocessor::macro_end(bool IncludeExternalMacros) const {
284 if (IncludeExternalMacros && ExternalSource &&
Douglas Gregor9882a5a2010-01-04 19:18:44 +0000285 !ReadMacrosFromExternalSource) {
286 ReadMacrosFromExternalSource = true;
287 ExternalSource->ReadDefinedMacros();
288 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000289
290 return Macros.end();
Douglas Gregor9882a5a2010-01-04 19:18:44 +0000291}
292
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000293/// \brief Compares macro tokens with a specified token value sequence.
294static bool MacroDefinitionEquals(const MacroInfo *MI,
295 llvm::ArrayRef<TokenValue> Tokens) {
296 return Tokens.size() == MI->getNumTokens() &&
297 std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin());
298}
299
300StringRef Preprocessor::getLastMacroWithSpelling(
301 SourceLocation Loc,
302 ArrayRef<TokenValue> Tokens) const {
303 SourceLocation BestLocation;
304 StringRef BestSpelling;
305 for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end();
306 I != E; ++I) {
307 if (!I->second->isObjectLike())
308 continue;
309 const MacroInfo *MI = I->second->findDefinitionAtLoc(Loc, SourceMgr);
310 if (!MI)
311 continue;
312 if (!MacroDefinitionEquals(MI, Tokens))
313 continue;
314 SourceLocation Location = I->second->getDefinitionLoc();
315 // Choose the macro defined latest.
316 if (BestLocation.isInvalid() ||
317 (Location.isValid() &&
318 SourceMgr.isBeforeInTranslationUnit(BestLocation, Location))) {
319 BestLocation = Location;
320 BestSpelling = I->first->getName();
321 }
322 }
323 return BestSpelling;
324}
325
Douglas Gregor8d76cca2012-01-04 06:20:15 +0000326void Preprocessor::recomputeCurLexerKind() {
327 if (CurLexer)
328 CurLexerKind = CLK_Lexer;
329 else if (CurPTHLexer)
330 CurLexerKind = CLK_PTHLexer;
331 else if (CurTokenLexer)
332 CurLexerKind = CLK_TokenLexer;
333 else
334 CurLexerKind = CLK_CachingLexer;
335}
336
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000337bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000338 unsigned CompleteLine,
339 unsigned CompleteColumn) {
340 assert(File);
341 assert(CompleteLine && CompleteColumn && "Starts from 1:1");
342 assert(!CodeCompletionFile && "Already set");
343
Douglas Gregor53ad6b92009-12-02 06:49:09 +0000344 using llvm::MemoryBuffer;
345
Douglas Gregor53ad6b92009-12-02 06:49:09 +0000346 // Load the actual file's contents.
Douglas Gregor26266da2010-03-16 19:49:24 +0000347 bool Invalid = false;
348 const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
349 if (Invalid)
Douglas Gregor53ad6b92009-12-02 06:49:09 +0000350 return true;
351
352 // Find the byte position of the truncation point.
353 const char *Position = Buffer->getBufferStart();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000354 for (unsigned Line = 1; Line < CompleteLine; ++Line) {
Douglas Gregor53ad6b92009-12-02 06:49:09 +0000355 for (; *Position; ++Position) {
356 if (*Position != '\r' && *Position != '\n')
357 continue;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000358
Douglas Gregor53ad6b92009-12-02 06:49:09 +0000359 // Eat \r\n or \n\r as a single line.
360 if ((Position[1] == '\r' || Position[1] == '\n') &&
361 Position[0] != Position[1])
362 ++Position;
363 ++Position;
364 break;
365 }
366 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000367
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000368 Position += CompleteColumn - 1;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000369
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000370 // Insert '\0' at the code-completion point.
Douglas Gregor9291ab62009-12-08 21:45:46 +0000371 if (Position < Buffer->getBufferEnd()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000372 CodeCompletionFile = File;
373 CodeCompletionOffset = Position - Buffer->getBufferStart();
374
375 MemoryBuffer *NewBuffer =
376 MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1,
377 Buffer->getBufferIdentifier());
Benjamin Kramer60053cf2011-09-04 20:26:28 +0000378 char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000379 char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
380 *NewPos = '\0';
381 std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
382 SourceMgr.overrideFileContents(File, NewBuffer);
Douglas Gregor53ad6b92009-12-02 06:49:09 +0000383 }
384
385 return false;
386}
387
Douglas Gregor11583702010-08-25 17:04:25 +0000388void Preprocessor::CodeCompleteNaturalLanguage() {
Douglas Gregor11583702010-08-25 17:04:25 +0000389 if (CodeComplete)
390 CodeComplete->CodeCompleteNaturalLanguage();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000391 setCodeCompletionReached();
Douglas Gregor11583702010-08-25 17:04:25 +0000392}
393
Benjamin Kramera197fb62010-02-27 17:05:45 +0000394/// getSpelling - This method is used to get the spelling of a token into a
395/// SmallVector. Note that the returned StringRef may not point to the
396/// supplied buffer if a copy can be avoided.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000397StringRef Preprocessor::getSpelling(const Token &Tok,
398 SmallVectorImpl<char> &Buffer,
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000399 bool *Invalid) const {
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000400 // NOTE: this has to be checked *before* testing for an IdentifierInfo.
401 if (Tok.isNot(tok::raw_identifier)) {
402 // Try the fast path.
403 if (const IdentifierInfo *II = Tok.getIdentifierInfo())
404 return II->getName();
405 }
Benjamin Kramera197fb62010-02-27 17:05:45 +0000406
407 // Resize the buffer if we need to copy into it.
408 if (Tok.needsCleaning())
409 Buffer.resize(Tok.getLength());
410
411 const char *Ptr = Buffer.data();
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000412 unsigned Len = getSpelling(Tok, Ptr, Invalid);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000413 return StringRef(Ptr, Len);
Benjamin Kramera197fb62010-02-27 17:05:45 +0000414}
415
Chris Lattnerb94ec7b2006-07-14 06:54:10 +0000416/// CreateString - Plop the specified string into a scratch buffer and return a
417/// location for it. If specified, the source location provides a source
418/// location for the token.
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000419void Preprocessor::CreateString(StringRef Str, Token &Tok,
Abramo Bagnarae398e602011-10-03 18:39:03 +0000420 SourceLocation ExpansionLocStart,
421 SourceLocation ExpansionLocEnd) {
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000422 Tok.setLength(Str.size());
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattner5a7971e2009-01-26 19:29:26 +0000424 const char *DestPtr;
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000425 SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000426
Abramo Bagnarae398e602011-10-03 18:39:03 +0000427 if (ExpansionLocStart.isValid())
428 Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart,
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000429 ExpansionLocEnd, Str.size());
Chris Lattner5a7971e2009-01-26 19:29:26 +0000430 Tok.setLocation(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000431
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000432 // If this is a raw identifier or a literal token, set the pointer data.
433 if (Tok.is(tok::raw_identifier))
434 Tok.setRawIdentifierData(DestPtr);
435 else if (Tok.isLiteral())
Chris Lattner5a7971e2009-01-26 19:29:26 +0000436 Tok.setLiteralData(DestPtr);
Chris Lattnerb94ec7b2006-07-14 06:54:10 +0000437}
438
Douglas Gregor2b82c2a2011-12-02 01:47:07 +0000439Module *Preprocessor::getCurrentModule() {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000440 if (getLangOpts().CurrentModule.empty())
Douglas Gregor2b82c2a2011-12-02 01:47:07 +0000441 return 0;
442
David Blaikiebbafb8a2012-03-11 07:00:24 +0000443 return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule);
Douglas Gregor2b82c2a2011-12-02 01:47:07 +0000444}
Chris Lattner8a7003c2007-07-16 06:48:38 +0000445
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000446//===----------------------------------------------------------------------===//
447// Preprocessor Initialization Methods
448//===----------------------------------------------------------------------===//
449
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000450
451/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begemanf7c3ff62008-01-07 04:01:26 +0000452/// which implicitly adds the builtin defines etc.
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000453void Preprocessor::EnterMainSourceFile() {
Chris Lattner9ef847b2009-02-13 19:33:24 +0000454 // We do not allow the preprocessor to reenter the main file. Doing so will
455 // cause FileID's to accumulate information from both runs (e.g. #line
456 // information) and predefined macros aren't guaranteed to be set properly.
457 assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
Chris Lattnerd32480d2009-01-17 06:22:33 +0000458 FileID MainFileID = SourceMgr.getMainFileID();
Mike Stump11289f42009-09-09 15:08:12 +0000459
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +0000460 // If MainFileID is loaded it means we loaded an AST file, no need to enter
461 // a main file.
462 if (!SourceMgr.isLoadedFileID(MainFileID)) {
463 // Enter the main file source buffer.
464 EnterSourceFile(MainFileID, 0, SourceLocation());
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000465
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +0000466 // If we've been asked to skip bytes in the main file (e.g., as part of a
467 // precompiled preamble), do so now.
468 if (SkipMainFilePreamble.first > 0)
469 CurLexer->SkipBytes(SkipMainFilePreamble.first,
470 SkipMainFilePreamble.second);
471
472 // Tell the header info that the main file was entered. If the file is later
473 // #imported, it won't be re-entered.
474 if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
475 HeaderInfo.IncrementIncludeCount(FE);
476 }
Mike Stump11289f42009-09-09 15:08:12 +0000477
Benjamin Kramerd77adb52009-12-31 15:33:09 +0000478 // Preprocess Predefines to populate the initial preprocessor state.
Mike Stump11289f42009-09-09 15:08:12 +0000479 llvm::MemoryBuffer *SB =
Chris Lattner58c79342010-04-05 22:42:27 +0000480 llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
Douglas Gregor33551892010-08-26 14:07:34 +0000481 assert(SB && "Cannot create predefined source buffer");
Meador Ingecdc00572012-06-19 18:17:30 +0000482 FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
Chris Lattnerd32480d2009-01-17 06:22:33 +0000483 assert(!FID.isInvalid() && "Could not create FileID for predefines?");
Mike Stump11289f42009-09-09 15:08:12 +0000484
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000485 // Start parsing the predefines.
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000486 EnterSourceFile(FID, 0, SourceLocation());
Chris Lattner1f1b0db2007-10-09 22:10:18 +0000487}
Chris Lattner8a7003c2007-07-16 06:48:38 +0000488
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000489void Preprocessor::EndSourceFile() {
490 // Notify the client that we reached the end of the source file.
491 if (Callbacks)
492 Callbacks->EndOfMainFile();
493}
Chris Lattner677757a2006-06-28 05:26:32 +0000494
495//===----------------------------------------------------------------------===//
496// Lexer Event Handling.
497//===----------------------------------------------------------------------===//
498
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000499/// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
500/// identifier information for the token and install it into the token,
501/// updating the token kind accordingly.
502IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
503 assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!");
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattnercefc7682006-07-08 08:28:12 +0000505 // Look up this token, see if it is a macro, or if it is a language keyword.
506 IdentifierInfo *II;
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000507 if (!Identifier.needsCleaning()) {
Chris Lattnercefc7682006-07-08 08:28:12 +0000508 // No cleaning needed, just use the characters from the lexed buffer.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000509 II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(),
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000510 Identifier.getLength()));
Chris Lattnercefc7682006-07-08 08:28:12 +0000511 } else {
512 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000513 SmallString<64> IdentifierBuffer;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000514 StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
Benjamin Kramer0a1abd42010-02-27 13:44:12 +0000515 II = getIdentifierInfo(CleanedStr);
Chris Lattnercefc7682006-07-08 08:28:12 +0000516 }
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000517
518 // Update the token info (identifier info and appropriate token kind).
Chris Lattner8c204872006-10-14 05:19:21 +0000519 Identifier.setIdentifierInfo(II);
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000520 Identifier.setKind(II->getTokenID());
521
Chris Lattnercefc7682006-07-08 08:28:12 +0000522 return II;
523}
524
John Wiegley1c0675e2011-04-28 01:08:34 +0000525void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) {
526 PoisonReasons[II] = DiagID;
527}
528
529void Preprocessor::PoisonSEHIdentifiers(bool Poison) {
530 assert(Ident__exception_code && Ident__exception_info);
531 assert(Ident___exception_code && Ident___exception_info);
532 Ident__exception_code->setIsPoisoned(Poison);
533 Ident___exception_code->setIsPoisoned(Poison);
534 Ident_GetExceptionCode->setIsPoisoned(Poison);
535 Ident__exception_info->setIsPoisoned(Poison);
536 Ident___exception_info->setIsPoisoned(Poison);
537 Ident_GetExceptionInfo->setIsPoisoned(Poison);
538 Ident__abnormal_termination->setIsPoisoned(Poison);
539 Ident___abnormal_termination->setIsPoisoned(Poison);
540 Ident_AbnormalTermination->setIsPoisoned(Poison);
541}
542
543void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) {
544 assert(Identifier.getIdentifierInfo() &&
545 "Can't handle identifiers without identifier info!");
546 llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it =
547 PoisonReasons.find(Identifier.getIdentifierInfo());
548 if(it == PoisonReasons.end())
549 Diag(Identifier, diag::err_pp_used_poisoned_id);
550 else
551 Diag(Identifier,it->second) << Identifier.getIdentifierInfo();
552}
Chris Lattnercefc7682006-07-08 08:28:12 +0000553
Chris Lattner677757a2006-06-28 05:26:32 +0000554/// HandleIdentifier - This callback is invoked when the lexer reads an
555/// identifier. This callback looks up the identifier in the map and/or
556/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnerad89ec02009-01-21 07:43:11 +0000557///
558/// Note that callers of this method are guarded by checking the
559/// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the
560/// IdentifierInfo methods that compute these properties will need to change to
561/// match.
Chris Lattner146762e2007-07-20 16:59:19 +0000562void Preprocessor::HandleIdentifier(Token &Identifier) {
Chris Lattner0f1f5052006-07-20 04:16:23 +0000563 assert(Identifier.getIdentifierInfo() &&
564 "Can't handle identifiers without identifier info!");
Mike Stump11289f42009-09-09 15:08:12 +0000565
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000566 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +0000567
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000568 // If the information about this identifier is out of date, update it from
569 // the external source.
Douglas Gregor3f568c12012-06-29 18:27:59 +0000570 // We have to treat __VA_ARGS__ in a special way, since it gets
571 // serialized with isPoisoned = true, but our preprocessor may have
572 // unpoisoned it if we're defining a C99 macro.
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000573 if (II.isOutOfDate()) {
Douglas Gregor3f568c12012-06-29 18:27:59 +0000574 bool CurrentIsPoisoned = false;
575 if (&II == Ident__VA_ARGS__)
576 CurrentIsPoisoned = Ident__VA_ARGS__->isPoisoned();
577
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000578 ExternalSource->updateOutOfDateIdentifier(II);
579 Identifier.setKind(II.getTokenID());
Douglas Gregor3f568c12012-06-29 18:27:59 +0000580
581 if (&II == Ident__VA_ARGS__)
582 II.setIsPoisoned(CurrentIsPoisoned);
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000583 }
584
Chris Lattner677757a2006-06-28 05:26:32 +0000585 // If this identifier was poisoned, and if it was not produced from a macro
586 // expansion, emit an error.
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000587 if (II.isPoisoned() && CurPPLexer) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000588 HandlePoisonedIdentifier(Identifier);
Chris Lattner8ff71992006-07-06 05:17:39 +0000589 }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Chris Lattner78186052006-07-09 00:45:31 +0000591 // If this is a macro to be expanded, do it.
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000592 if (MacroInfo *MI = getMacroInfo(&II)) {
Abramo Bagnara123bec82012-01-01 22:01:04 +0000593 if (!DisableMacroExpansion) {
594 if (Identifier.isExpandDisabled()) {
595 Diag(Identifier, diag::pp_disabled_macro_expansion);
596 } else if (MI->isEnabled()) {
Chris Lattner6e4bf522006-07-27 06:59:25 +0000597 if (!HandleMacroExpandedIdentifier(Identifier, MI))
Douglas Gregord90c3c92011-08-27 06:37:51 +0000598 return;
Chris Lattner6e4bf522006-07-27 06:59:25 +0000599 } else {
600 // C99 6.10.3.4p2 says that a disabled macro may never again be
601 // expanded, even if it's in a context where it could be expanded in the
602 // future.
Chris Lattner146762e2007-07-20 16:59:19 +0000603 Identifier.setFlag(Token::DisableExpand);
Abramo Bagnara123bec82012-01-01 22:01:04 +0000604 Diag(Identifier, diag::pp_disabled_macro_expansion);
Chris Lattner6e4bf522006-07-27 06:59:25 +0000605 }
606 }
Chris Lattner063400e2006-10-14 19:54:15 +0000607 }
Chris Lattner677757a2006-06-28 05:26:32 +0000608
Richard Smith4dd85d62011-10-11 19:57:52 +0000609 // If this identifier is a keyword in C++11, produce a warning. Don't warn if
610 // we're not considering macro expansion, since this identifier might be the
611 // name of a macro.
612 // FIXME: This warning is disabled in cases where it shouldn't be, like
613 // "#define constexpr constexpr", "int constexpr;"
614 if (II.isCXX11CompatKeyword() & !DisableMacroExpansion) {
615 Diag(Identifier, diag::warn_cxx11_keyword) << II.getName();
616 // Don't diagnose this keyword again in this translation unit.
617 II.setIsCXX11CompatKeyword(false);
618 }
619
Chris Lattner5b9f4892006-11-21 17:23:33 +0000620 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
621 // then we act as if it is the actual operator and not the textual
622 // representation of it.
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000623 if (II.isCPlusPlusOperatorKeyword())
Chris Lattner5b9f4892006-11-21 17:23:33 +0000624 Identifier.setIdentifierInfo(0);
625
Chris Lattner677757a2006-06-28 05:26:32 +0000626 // If this is an extension token, diagnose its use.
Steve Naroffc84e8b72008-09-02 18:50:17 +0000627 // We avoid diagnosing tokens that originate from macro definitions.
Eli Friedman6bba2ad2009-04-28 03:59:15 +0000628 // FIXME: This warning is disabled in cases where it shouldn't be,
629 // like "#define TY typeof", "TY(1) x".
630 if (II.isExtensionToken() && !DisableMacroExpansion)
Chris Lattner53621a52007-06-13 20:44:40 +0000631 Diag(Identifier, diag::ext_token_used);
Douglas Gregoraf5c4842011-09-07 23:11:54 +0000632
Ted Kremenekc1e4dd02012-03-01 22:07:04 +0000633 // If this is the '__experimental_modules_import' contextual keyword, note
634 // that the next token indicates a module name.
Douglas Gregor8d76cca2012-01-04 06:20:15 +0000635 //
Ted Kremenekc1e4dd02012-03-01 22:07:04 +0000636 // Note that we do not treat '__experimental_modules_import' as a contextual
637 // keyword when we're in a caching lexer, because caching lexers only get
638 // used in contexts where import declarations are disallowed.
639 if (II.isModulesImport() && !InMacroArgs && !DisableMacroExpansion &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000640 getLangOpts().Modules && CurLexerKind != CLK_CachingLexer) {
Douglas Gregoraf5c4842011-09-07 23:11:54 +0000641 ModuleImportLoc = Identifier.getLocation();
Douglas Gregor1805b8a2011-11-30 04:26:53 +0000642 ModuleImportPath.clear();
643 ModuleImportExpectsIdentifier = true;
Douglas Gregoraf5c4842011-09-07 23:11:54 +0000644 CurLexerKind = CLK_LexAfterModuleImport;
645 }
Douglas Gregor08142532011-08-26 23:56:07 +0000646}
647
Douglas Gregorda82e702012-01-03 19:32:59 +0000648/// \brief Lex a token following the 'import' contextual keyword.
Douglas Gregor22d09742012-01-03 18:04:46 +0000649///
Douglas Gregoraf5c4842011-09-07 23:11:54 +0000650void Preprocessor::LexAfterModuleImport(Token &Result) {
651 // Figure out what kind of lexer we actually have.
Douglas Gregor8d76cca2012-01-04 06:20:15 +0000652 recomputeCurLexerKind();
Douglas Gregoraf5c4842011-09-07 23:11:54 +0000653
654 // Lex the next token.
655 Lex(Result);
656
Douglas Gregor08142532011-08-26 23:56:07 +0000657 // The token sequence
658 //
Douglas Gregor22d09742012-01-03 18:04:46 +0000659 // import identifier (. identifier)*
660 //
Douglas Gregorda82e702012-01-03 19:32:59 +0000661 // indicates a module import directive. We already saw the 'import'
662 // contextual keyword, so now we're looking for the identifiers.
Douglas Gregor1805b8a2011-11-30 04:26:53 +0000663 if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) {
664 // We expected to see an identifier here, and we did; continue handling
665 // identifiers.
666 ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(),
667 Result.getLocation()));
668 ModuleImportExpectsIdentifier = false;
669 CurLexerKind = CLK_LexAfterModuleImport;
Douglas Gregor08142532011-08-26 23:56:07 +0000670 return;
Douglas Gregor1805b8a2011-11-30 04:26:53 +0000671 }
Douglas Gregor08142532011-08-26 23:56:07 +0000672
Douglas Gregor1805b8a2011-11-30 04:26:53 +0000673 // If we're expecting a '.' or a ';', and we got a '.', then wait until we
674 // see the next identifier.
675 if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) {
676 ModuleImportExpectsIdentifier = true;
677 CurLexerKind = CLK_LexAfterModuleImport;
678 return;
679 }
680
681 // If we have a non-empty module path, load the named module.
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +0000682 if (!ModuleImportPath.empty()) {
683 Module *Imported = TheModuleLoader.loadModule(ModuleImportLoc,
684 ModuleImportPath,
685 Module::MacrosVisible,
686 /*IsIncludeDirective=*/false);
687 if (Callbacks)
688 Callbacks->moduleImport(ModuleImportLoc, ModuleImportPath, Imported);
689 }
Chris Lattner677757a2006-06-28 05:26:32 +0000690}
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000691
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000692void Preprocessor::addCommentHandler(CommentHandler *Handler) {
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000693 assert(Handler && "NULL comment handler");
694 assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) ==
695 CommentHandlers.end() && "Comment handler already registered");
696 CommentHandlers.push_back(Handler);
697}
698
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000699void Preprocessor::removeCommentHandler(CommentHandler *Handler) {
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000700 std::vector<CommentHandler *>::iterator Pos
701 = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler);
702 assert(Pos != CommentHandlers.end() && "Comment handler not registered");
703 CommentHandlers.erase(Pos);
704}
705
Chris Lattner87d02082010-01-18 22:35:47 +0000706bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
707 bool AnyPendingTokens = false;
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000708 for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
709 HEnd = CommentHandlers.end();
Chris Lattner87d02082010-01-18 22:35:47 +0000710 H != HEnd; ++H) {
711 if ((*H)->HandleComment(*this, Comment))
712 AnyPendingTokens = true;
713 }
714 if (!AnyPendingTokens || getCommentRetentionState())
715 return false;
716 Lex(result);
717 return true;
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000718}
719
Douglas Gregor08142532011-08-26 23:56:07 +0000720ModuleLoader::~ModuleLoader() { }
721
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000722CommentHandler::~CommentHandler() { }
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000723
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000724CodeCompletionHandler::~CodeCompletionHandler() { }
725
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000726void Preprocessor::createPreprocessingRecord(bool RecordConditionalDirectives) {
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000727 if (Record)
728 return;
729
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000730 Record = new PreprocessingRecord(getSourceManager(),
731 RecordConditionalDirectives);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000732 addPPCallbacks(Record);
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000733}