blob: 9e43d6ac5668d4df14205155b418586c9090003b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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.
Chris Lattnerf73903a2009-02-06 06:45:26 +000016// -d[DNI] - Dump various things.
Reid Spencer5f016e22007-07-11 17:01:13 +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//
26//===----------------------------------------------------------------------===//
27
28#include "clang/Lex/Preprocessor.h"
Chris Lattner23f77e52009-12-15 01:51:03 +000029#include "MacroArgs.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "clang/Basic/FileManager.h"
31#include "clang/Basic/SourceManager.h"
32#include "clang/Basic/TargetInfo.h"
33#include "clang/Lex/CodeCompletionHandler.h"
Douglas Gregor88a35862010-01-04 19:18:44 +000034#include "clang/Lex/ExternalPreprocessorSource.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000035#include "clang/Lex/HeaderSearch.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000036#include "clang/Lex/LexDiagnostic.h"
37#include "clang/Lex/LiteralSupport.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000038#include "clang/Lex/MacroInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000039#include "clang/Lex/ModuleLoader.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000040#include "clang/Lex/Pragma.h"
Douglas Gregor94dc8f62010-03-19 16:15:56 +000041#include "clang/Lex/PreprocessingRecord.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000042#include "clang/Lex/PreprocessorOptions.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000043#include "clang/Lex/ScratchBuffer.h"
Chris Lattner2db78dd2008-10-05 20:40:30 +000044#include "llvm/ADT/APFloat.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000045#include "llvm/ADT/SmallString.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000046#include "llvm/Support/Capacity.h"
Chris Lattner97ba77c2007-07-16 06:48:38 +000047#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000048#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000049using namespace clang;
50
51//===----------------------------------------------------------------------===//
Douglas Gregor88a35862010-01-04 19:18:44 +000052ExternalPreprocessorSource::~ExternalPreprocessorSource() { }
Reid Spencer5f016e22007-07-11 17:01:13 +000053
Douglas Gregora8235d62012-10-09 23:05:51 +000054PPMutationListener::~PPMutationListener() { }
55
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000056Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts,
Douglas Gregor36a16492012-10-24 17:46:57 +000057 DiagnosticsEngine &diags, LangOptions &opts,
Douglas Gregor998b3d32011-09-01 23:39:15 +000058 const TargetInfo *target, SourceManager &SM,
Douglas Gregor6aa52ec2011-08-26 23:56:07 +000059 HeaderSearch &Headers, ModuleLoader &TheModuleLoader,
David Blaikie49446062013-01-16 23:13:36 +000060 IdentifierInfoLookup *IILookup, bool OwnsHeaders,
61 bool DelayInitialization, bool IncrProcessing)
62 : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), Target(target),
63 FileMgr(Headers.getFileMgr()), SourceMgr(SM), HeaderInfo(Headers),
64 TheModuleLoader(TheModuleLoader), ExternalSource(0),
65 ParsingIfOrElifDirective(false), Identifiers(opts, IILookup),
66 IncrementalProcessing(IncrProcessing), CodeComplete(0),
67 CodeCompletionFile(0), CodeCompletionOffset(0), CodeCompletionReached(0),
68 SkipMainFilePreamble(0, true), CurPPLexer(0), CurDirLookup(0),
69 CurLexerKind(CLK_Lexer), Callbacks(0), Listener(0), MacroArgCache(0),
70 Record(0), MIChainHead(0), MICache(0) {
Daniel Dunbar5814e652009-11-11 21:44:21 +000071 OwnsHeaderSearch = OwnsHeaders;
Douglas Gregor998b3d32011-09-01 23:39:15 +000072
73 ScratchBuf = new ScratchBuffer(SourceMgr);
74 CounterValue = 0; // __COUNTER__ starts at 0.
75
76 // Clear stats.
77 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
78 NumIf = NumElse = NumEndif = 0;
79 NumEnteredSourceFiles = 0;
80 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
81 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
82 MaxIncludeStackDepth = 0;
83 NumSkipped = 0;
84
85 // Default to discarding comments.
86 KeepComments = false;
87 KeepMacroComments = false;
88 SuppressIncludeNotFoundError = false;
89
90 // Macro expansion is enabled.
91 DisableMacroExpansion = false;
David Blaikie8c0b3782012-06-06 18:52:13 +000092 MacroExpansionInDirectivesOverride = false;
Douglas Gregor998b3d32011-09-01 23:39:15 +000093 InMacroArgs = false;
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +000094 InMacroArgPreExpansion = false;
Douglas Gregor998b3d32011-09-01 23:39:15 +000095 NumCachedTokenLexers = 0;
Jordan Rose6fe6a492012-06-08 18:06:21 +000096 PragmasEnabled = true;
Eric Christopher214ea9d2013-01-16 20:09:36 +000097 ParsingIfOrElifDirective = false;
Jordan Rose6fe6a492012-06-08 18:06:21 +000098
Douglas Gregor998b3d32011-09-01 23:39:15 +000099 CachedLexPos = 0;
Eric Christopher214ea9d2013-01-16 20:09:36 +0000100
Douglas Gregor998b3d32011-09-01 23:39:15 +0000101 // We haven't read anything from the external source.
102 ReadMacrosFromExternalSource = false;
103
Douglas Gregor998b3d32011-09-01 23:39:15 +0000104 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
105 // This gets unpoisoned where it is allowed.
106 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
107 SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use);
108
109 // Initialize the pragma handlers.
110 PragmaHandlers = new PragmaNamespace(StringRef());
111 RegisterBuiltinPragmas();
112
113 // Initialize builtin macros like __LINE__ and friends.
114 RegisterBuiltinMacros();
115
David Blaikie4e4d0842012-03-11 07:00:24 +0000116 if(LangOpts.Borland) {
Douglas Gregor998b3d32011-09-01 23:39:15 +0000117 Ident__exception_info = getIdentifierInfo("_exception_info");
118 Ident___exception_info = getIdentifierInfo("__exception_info");
119 Ident_GetExceptionInfo = getIdentifierInfo("GetExceptionInformation");
120 Ident__exception_code = getIdentifierInfo("_exception_code");
121 Ident___exception_code = getIdentifierInfo("__exception_code");
122 Ident_GetExceptionCode = getIdentifierInfo("GetExceptionCode");
123 Ident__abnormal_termination = getIdentifierInfo("_abnormal_termination");
124 Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination");
125 Ident_AbnormalTermination = getIdentifierInfo("AbnormalTermination");
126 } else {
127 Ident__exception_info = Ident__exception_code = Ident__abnormal_termination = 0;
128 Ident___exception_info = Ident___exception_code = Ident___abnormal_termination = 0;
129 Ident_GetExceptionInfo = Ident_GetExceptionCode = Ident_AbnormalTermination = 0;
Douglas Gregordc58aa72012-01-30 06:01:29 +0000130 }
Argyrios Kyrtzidis25c25962012-06-02 18:08:09 +0000131
132 if (!DelayInitialization) {
133 assert(Target && "Must provide target information for PP initialization");
134 Initialize(*Target);
135 }
136}
137
138Preprocessor::~Preprocessor() {
139 assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
140
141 while (!IncludeMacroStack.empty()) {
142 delete IncludeMacroStack.back().TheLexer;
143 delete IncludeMacroStack.back().TheTokenLexer;
144 IncludeMacroStack.pop_back();
145 }
146
147 // Free any macro definitions.
148 for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next)
149 I->MI.Destroy();
150
151 // Free any cached macro expanders.
152 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
153 delete TokenLexerCache[i];
154
155 // Free any cached MacroArgs.
156 for (MacroArgs *ArgList = MacroArgCache; ArgList; )
157 ArgList = ArgList->deallocate();
158
159 // Release pragma information.
160 delete PragmaHandlers;
161
162 // Delete the scratch buffer info.
163 delete ScratchBuf;
164
165 // Delete the header search info, if we own it.
166 if (OwnsHeaderSearch)
167 delete &HeaderInfo;
168
169 delete Callbacks;
170}
171
172void Preprocessor::Initialize(const TargetInfo &Target) {
173 assert((!this->Target || this->Target == &Target) &&
174 "Invalid override of target information");
175 this->Target = &Target;
Douglas Gregordc58aa72012-01-30 06:01:29 +0000176
Argyrios Kyrtzidis25c25962012-06-02 18:08:09 +0000177 // Initialize information about built-ins.
178 BuiltinInfo.InitializeTarget(Target);
Douglas Gregordc58aa72012-01-30 06:01:29 +0000179 HeaderInfo.setTarget(Target);
Douglas Gregor998b3d32011-09-01 23:39:15 +0000180}
181
Ted Kremenek337edcd2009-02-12 03:26:59 +0000182void Preprocessor::setPTHManager(PTHManager* pm) {
183 PTH.reset(pm);
Douglas Gregor52e71082009-10-16 18:18:30 +0000184 FileMgr.addStatCache(PTH->createStatCache());
Ted Kremenek337edcd2009-02-12 03:26:59 +0000185}
186
Chris Lattnerd2177732007-07-20 16:59:19 +0000187void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000188 llvm::errs() << tok::getTokenName(Tok.getKind()) << " '"
189 << getSpelling(Tok) << "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 if (!DumpFlags) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000193 llvm::errs() << "\t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 if (Tok.isAtStartOfLine())
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000195 llvm::errs() << " [StartOfLine]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 if (Tok.hasLeadingSpace())
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000197 llvm::errs() << " [LeadingSpace]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 if (Tok.isExpandDisabled())
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000199 llvm::errs() << " [ExpandDisabled]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 if (Tok.needsCleaning()) {
201 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000202 llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength())
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000203 << "']";
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000206 llvm::errs() << "\tLoc=<";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000207 DumpLocation(Tok.getLocation());
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000208 llvm::errs() << ">";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000209}
210
211void Preprocessor::DumpLocation(SourceLocation Loc) const {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000212 Loc.dump(SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000213}
214
215void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000216 llvm::errs() << "MACRO: ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
218 DumpToken(MI.getReplacementToken(i));
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000219 llvm::errs() << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 }
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000221 llvm::errs() << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000222}
223
224void Preprocessor::PrintStats() {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000225 llvm::errs() << "\n*** Preprocessor Stats:\n";
226 llvm::errs() << NumDirectives << " directives found:\n";
227 llvm::errs() << " " << NumDefined << " #define.\n";
228 llvm::errs() << " " << NumUndefined << " #undef.\n";
229 llvm::errs() << " #include/#include_next/#import:\n";
230 llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n";
231 llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n";
232 llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n";
233 llvm::errs() << " " << NumElse << " #else/#elif.\n";
234 llvm::errs() << " " << NumEndif << " #endif.\n";
235 llvm::errs() << " " << NumPragma << " #pragma.\n";
236 llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000237
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000238 llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000239 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
240 << NumFastMacroExpanded << " on the fast path.\n";
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000241 llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000242 << " token paste (##) operations performed, "
243 << NumFastTokenPaste << " on the fast path.\n";
Alexander Kornienkocd6df662012-08-13 10:46:42 +0000244
245 llvm::errs() << "\nPreprocessor Memory: " << getTotalMemory() << "B total";
246
247 llvm::errs() << "\n BumpPtr: " << BP.getTotalMemory();
248 llvm::errs() << "\n Macro Expanded Tokens: "
249 << llvm::capacity_in_bytes(MacroExpandedTokens);
250 llvm::errs() << "\n Predefines Buffer: " << Predefines.capacity();
251 llvm::errs() << "\n Macros: " << llvm::capacity_in_bytes(Macros);
252 llvm::errs() << "\n #pragma push_macro Info: "
253 << llvm::capacity_in_bytes(PragmaPushMacroInfo);
254 llvm::errs() << "\n Poison Reasons: "
255 << llvm::capacity_in_bytes(PoisonReasons);
256 llvm::errs() << "\n Comment Handlers: "
257 << llvm::capacity_in_bytes(CommentHandlers) << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000260Preprocessor::macro_iterator
261Preprocessor::macro_begin(bool IncludeExternalMacros) const {
262 if (IncludeExternalMacros && ExternalSource &&
Douglas Gregor88a35862010-01-04 19:18:44 +0000263 !ReadMacrosFromExternalSource) {
264 ReadMacrosFromExternalSource = true;
265 ExternalSource->ReadDefinedMacros();
266 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000267
268 return Macros.begin();
Douglas Gregor88a35862010-01-04 19:18:44 +0000269}
270
Argyrios Kyrtzidisc5c5e922011-06-29 22:20:04 +0000271size_t Preprocessor::getTotalMemory() const {
Ted Kremenek91d1bd62011-07-26 21:17:24 +0000272 return BP.getTotalMemory()
Ted Kremenek67485092011-07-27 18:41:23 +0000273 + llvm::capacity_in_bytes(MacroExpandedTokens)
Ted Kremenek91d1bd62011-07-26 21:17:24 +0000274 + Predefines.capacity() /* Predefines buffer. */
Ted Kremenek67485092011-07-27 18:41:23 +0000275 + llvm::capacity_in_bytes(Macros)
276 + llvm::capacity_in_bytes(PragmaPushMacroInfo)
277 + llvm::capacity_in_bytes(PoisonReasons)
278 + llvm::capacity_in_bytes(CommentHandlers);
Argyrios Kyrtzidisc5c5e922011-06-29 22:20:04 +0000279}
280
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000281Preprocessor::macro_iterator
282Preprocessor::macro_end(bool IncludeExternalMacros) const {
283 if (IncludeExternalMacros && ExternalSource &&
Douglas Gregor88a35862010-01-04 19:18:44 +0000284 !ReadMacrosFromExternalSource) {
285 ReadMacrosFromExternalSource = true;
286 ExternalSource->ReadDefinedMacros();
287 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000288
289 return Macros.end();
Douglas Gregor88a35862010-01-04 19:18:44 +0000290}
291
Dmitri Gribenko19523542012-09-29 11:40:46 +0000292/// \brief Compares macro tokens with a specified token value sequence.
293static bool MacroDefinitionEquals(const MacroInfo *MI,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000294 ArrayRef<TokenValue> Tokens) {
Dmitri Gribenko19523542012-09-29 11:40:46 +0000295 return Tokens.size() == MI->getNumTokens() &&
296 std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin());
297}
298
299StringRef Preprocessor::getLastMacroWithSpelling(
300 SourceLocation Loc,
301 ArrayRef<TokenValue> Tokens) const {
302 SourceLocation BestLocation;
303 StringRef BestSpelling;
304 for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end();
305 I != E; ++I) {
306 if (!I->second->isObjectLike())
307 continue;
308 const MacroInfo *MI = I->second->findDefinitionAtLoc(Loc, SourceMgr);
309 if (!MI)
310 continue;
311 if (!MacroDefinitionEquals(MI, Tokens))
312 continue;
313 SourceLocation Location = I->second->getDefinitionLoc();
314 // Choose the macro defined latest.
315 if (BestLocation.isInvalid() ||
316 (Location.isValid() &&
317 SourceMgr.isBeforeInTranslationUnit(BestLocation, Location))) {
318 BestLocation = Location;
319 BestSpelling = I->first->getName();
320 }
321 }
322 return BestSpelling;
323}
324
Douglas Gregord6aba062012-01-04 06:20:15 +0000325void Preprocessor::recomputeCurLexerKind() {
326 if (CurLexer)
327 CurLexerKind = CLK_Lexer;
328 else if (CurPTHLexer)
329 CurLexerKind = CLK_PTHLexer;
330 else if (CurTokenLexer)
331 CurLexerKind = CLK_TokenLexer;
332 else
333 CurLexerKind = CLK_CachingLexer;
334}
335
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000336bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000337 unsigned CompleteLine,
338 unsigned CompleteColumn) {
339 assert(File);
340 assert(CompleteLine && CompleteColumn && "Starts from 1:1");
341 assert(!CodeCompletionFile && "Already set");
342
Douglas Gregor29684422009-12-02 06:49:09 +0000343 using llvm::MemoryBuffer;
344
Douglas Gregor29684422009-12-02 06:49:09 +0000345 // Load the actual file's contents.
Douglas Gregoraa38c3d2010-03-16 19:49:24 +0000346 bool Invalid = false;
347 const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
348 if (Invalid)
Douglas Gregor29684422009-12-02 06:49:09 +0000349 return true;
350
351 // Find the byte position of the truncation point.
352 const char *Position = Buffer->getBufferStart();
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000353 for (unsigned Line = 1; Line < CompleteLine; ++Line) {
Douglas Gregor29684422009-12-02 06:49:09 +0000354 for (; *Position; ++Position) {
355 if (*Position != '\r' && *Position != '\n')
356 continue;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000357
Douglas Gregor29684422009-12-02 06:49:09 +0000358 // Eat \r\n or \n\r as a single line.
359 if ((Position[1] == '\r' || Position[1] == '\n') &&
360 Position[0] != Position[1])
361 ++Position;
362 ++Position;
363 break;
364 }
365 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000366
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000367 Position += CompleteColumn - 1;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000368
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000369 // Insert '\0' at the code-completion point.
Douglas Gregorb760fe82009-12-08 21:45:46 +0000370 if (Position < Buffer->getBufferEnd()) {
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000371 CodeCompletionFile = File;
372 CodeCompletionOffset = Position - Buffer->getBufferStart();
373
374 MemoryBuffer *NewBuffer =
375 MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1,
376 Buffer->getBufferIdentifier());
Benjamin Kramer41a50a92011-09-04 20:26:28 +0000377 char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000378 char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
379 *NewPos = '\0';
380 std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
381 SourceMgr.overrideFileContents(File, NewBuffer);
Douglas Gregor29684422009-12-02 06:49:09 +0000382 }
383
384 return false;
385}
386
Douglas Gregor55817af2010-08-25 17:04:25 +0000387void Preprocessor::CodeCompleteNaturalLanguage() {
Douglas Gregor55817af2010-08-25 17:04:25 +0000388 if (CodeComplete)
389 CodeComplete->CodeCompleteNaturalLanguage();
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000390 setCodeCompletionReached();
Douglas Gregor55817af2010-08-25 17:04:25 +0000391}
392
Benjamin Kramer51f5fe32010-02-27 17:05:45 +0000393/// getSpelling - This method is used to get the spelling of a token into a
394/// SmallVector. Note that the returned StringRef may not point to the
395/// supplied buffer if a copy can be avoided.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000396StringRef Preprocessor::getSpelling(const Token &Tok,
397 SmallVectorImpl<char> &Buffer,
Douglas Gregor50f6af72010-03-16 05:20:39 +0000398 bool *Invalid) const {
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000399 // NOTE: this has to be checked *before* testing for an IdentifierInfo.
400 if (Tok.isNot(tok::raw_identifier)) {
401 // Try the fast path.
402 if (const IdentifierInfo *II = Tok.getIdentifierInfo())
403 return II->getName();
404 }
Benjamin Kramer51f5fe32010-02-27 17:05:45 +0000405
406 // Resize the buffer if we need to copy into it.
407 if (Tok.needsCleaning())
408 Buffer.resize(Tok.getLength());
409
410 const char *Ptr = Buffer.data();
Douglas Gregor50f6af72010-03-16 05:20:39 +0000411 unsigned Len = getSpelling(Tok, Ptr, Invalid);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000412 return StringRef(Ptr, Len);
Benjamin Kramer51f5fe32010-02-27 17:05:45 +0000413}
414
Reid Spencer5f016e22007-07-11 17:01:13 +0000415/// CreateString - Plop the specified string into a scratch buffer and return a
416/// location for it. If specified, the source location provides a source
417/// location for the token.
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000418void Preprocessor::CreateString(StringRef Str, Token &Tok,
Abramo Bagnaraa08529c2011-10-03 18:39:03 +0000419 SourceLocation ExpansionLocStart,
420 SourceLocation ExpansionLocEnd) {
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000421 Tok.setLength(Str.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattner47246be2009-01-26 19:29:26 +0000423 const char *DestPtr;
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000424 SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Abramo Bagnaraa08529c2011-10-03 18:39:03 +0000426 if (ExpansionLocStart.isValid())
427 Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart,
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000428 ExpansionLocEnd, Str.size());
Chris Lattner47246be2009-01-26 19:29:26 +0000429 Tok.setLocation(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000431 // If this is a raw identifier or a literal token, set the pointer data.
432 if (Tok.is(tok::raw_identifier))
433 Tok.setRawIdentifierData(DestPtr);
434 else if (Tok.isLiteral())
Chris Lattner47246be2009-01-26 19:29:26 +0000435 Tok.setLiteralData(DestPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000436}
437
Douglas Gregor90db2602011-12-02 01:47:07 +0000438Module *Preprocessor::getCurrentModule() {
David Blaikie4e4d0842012-03-11 07:00:24 +0000439 if (getLangOpts().CurrentModule.empty())
Douglas Gregor90db2602011-12-02 01:47:07 +0000440 return 0;
441
David Blaikie4e4d0842012-03-11 07:00:24 +0000442 return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule);
Douglas Gregor90db2602011-12-02 01:47:07 +0000443}
Chris Lattner97ba77c2007-07-16 06:48:38 +0000444
Chris Lattner53b0dab2007-10-09 22:10:18 +0000445//===----------------------------------------------------------------------===//
446// Preprocessor Initialization Methods
447//===----------------------------------------------------------------------===//
448
Chris Lattner53b0dab2007-10-09 22:10:18 +0000449
450/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begeman6b616022008-01-07 04:01:26 +0000451/// which implicitly adds the builtin defines etc.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000452void Preprocessor::EnterMainSourceFile() {
Chris Lattner05db4272009-02-13 19:33:24 +0000453 // We do not allow the preprocessor to reenter the main file. Doing so will
454 // cause FileID's to accumulate information from both runs (e.g. #line
455 // information) and predefined macros aren't guaranteed to be set properly.
456 assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
Chris Lattner2b2453a2009-01-17 06:22:33 +0000457 FileID MainFileID = SourceMgr.getMainFileID();
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000459 // If MainFileID is loaded it means we loaded an AST file, no need to enter
460 // a main file.
461 if (!SourceMgr.isLoadedFileID(MainFileID)) {
462 // Enter the main file source buffer.
463 EnterSourceFile(MainFileID, 0, SourceLocation());
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000464
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000465 // If we've been asked to skip bytes in the main file (e.g., as part of a
466 // precompiled preamble), do so now.
467 if (SkipMainFilePreamble.first > 0)
468 CurLexer->SkipBytes(SkipMainFilePreamble.first,
469 SkipMainFilePreamble.second);
470
471 // Tell the header info that the main file was entered. If the file is later
472 // #imported, it won't be re-entered.
473 if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
474 HeaderInfo.IncrementIncludeCount(FE);
475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Benjamin Kramerffd6e392009-12-31 15:33:09 +0000477 // Preprocess Predefines to populate the initial preprocessor state.
Mike Stump1eb44332009-09-09 15:08:12 +0000478 llvm::MemoryBuffer *SB =
Chris Lattnera0a270c2010-04-05 22:42:27 +0000479 llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
Douglas Gregor043266b2010-08-26 14:07:34 +0000480 assert(SB && "Cannot create predefined source buffer");
Meador Inge9416d422012-06-19 18:17:30 +0000481 FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000482 assert(!FID.isInvalid() && "Could not create FileID for predefines?");
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner53b0dab2007-10-09 22:10:18 +0000484 // Start parsing the predefines.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000485 EnterSourceFile(FID, 0, SourceLocation());
Chris Lattner53b0dab2007-10-09 22:10:18 +0000486}
Chris Lattner97ba77c2007-07-16 06:48:38 +0000487
Daniel Dunbardbd82092010-03-23 05:09:10 +0000488void Preprocessor::EndSourceFile() {
489 // Notify the client that we reached the end of the source file.
490 if (Callbacks)
491 Callbacks->EndOfMainFile();
492}
Reid Spencer5f016e22007-07-11 17:01:13 +0000493
494//===----------------------------------------------------------------------===//
495// Lexer Event Handling.
496//===----------------------------------------------------------------------===//
497
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000498/// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
499/// identifier information for the token and install it into the token,
500/// updating the token kind accordingly.
501IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
502 assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!");
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 // Look up this token, see if it is a macro, or if it is a language keyword.
505 IdentifierInfo *II;
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000506 if (!Identifier.needsCleaning()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 // No cleaning needed, just use the characters from the lexed buffer.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000508 II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(),
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000509 Identifier.getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 } else {
511 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000512 SmallString<64> IdentifierBuffer;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000513 StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
Benjamin Kramerddeea562010-02-27 13:44:12 +0000514 II = getIdentifierInfo(CleanedStr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 }
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000516
517 // Update the token info (identifier info and appropriate token kind).
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 Identifier.setIdentifierInfo(II);
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000519 Identifier.setKind(II->getTokenID());
520
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 return II;
522}
523
John Wiegley28bbe4b2011-04-28 01:08:34 +0000524void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) {
525 PoisonReasons[II] = DiagID;
526}
527
528void Preprocessor::PoisonSEHIdentifiers(bool Poison) {
529 assert(Ident__exception_code && Ident__exception_info);
530 assert(Ident___exception_code && Ident___exception_info);
531 Ident__exception_code->setIsPoisoned(Poison);
532 Ident___exception_code->setIsPoisoned(Poison);
533 Ident_GetExceptionCode->setIsPoisoned(Poison);
534 Ident__exception_info->setIsPoisoned(Poison);
535 Ident___exception_info->setIsPoisoned(Poison);
536 Ident_GetExceptionInfo->setIsPoisoned(Poison);
537 Ident__abnormal_termination->setIsPoisoned(Poison);
538 Ident___abnormal_termination->setIsPoisoned(Poison);
539 Ident_AbnormalTermination->setIsPoisoned(Poison);
540}
541
542void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) {
543 assert(Identifier.getIdentifierInfo() &&
544 "Can't handle identifiers without identifier info!");
545 llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it =
546 PoisonReasons.find(Identifier.getIdentifierInfo());
547 if(it == PoisonReasons.end())
548 Diag(Identifier, diag::err_pp_used_poisoned_id);
549 else
550 Diag(Identifier,it->second) << Identifier.getIdentifierInfo();
551}
Reid Spencer5f016e22007-07-11 17:01:13 +0000552
553/// HandleIdentifier - This callback is invoked when the lexer reads an
554/// identifier. This callback looks up the identifier in the map and/or
555/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattner6a170eb2009-01-21 07:43:11 +0000556///
557/// Note that callers of this method are guarded by checking the
558/// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the
559/// IdentifierInfo methods that compute these properties will need to change to
560/// match.
Chris Lattnerd2177732007-07-20 16:59:19 +0000561void Preprocessor::HandleIdentifier(Token &Identifier) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 assert(Identifier.getIdentifierInfo() &&
563 "Can't handle identifiers without identifier info!");
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 IdentifierInfo &II = *Identifier.getIdentifierInfo();
566
Douglas Gregoreee242f2011-10-27 09:33:13 +0000567 // If the information about this identifier is out of date, update it from
568 // the external source.
Douglas Gregor193f91b2012-06-29 18:27:59 +0000569 // We have to treat __VA_ARGS__ in a special way, since it gets
570 // serialized with isPoisoned = true, but our preprocessor may have
571 // unpoisoned it if we're defining a C99 macro.
Douglas Gregoreee242f2011-10-27 09:33:13 +0000572 if (II.isOutOfDate()) {
Douglas Gregor193f91b2012-06-29 18:27:59 +0000573 bool CurrentIsPoisoned = false;
574 if (&II == Ident__VA_ARGS__)
575 CurrentIsPoisoned = Ident__VA_ARGS__->isPoisoned();
576
Douglas Gregoreee242f2011-10-27 09:33:13 +0000577 ExternalSource->updateOutOfDateIdentifier(II);
578 Identifier.setKind(II.getTokenID());
Douglas Gregor193f91b2012-06-29 18:27:59 +0000579
580 if (&II == Ident__VA_ARGS__)
581 II.setIsPoisoned(CurrentIsPoisoned);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000582 }
583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 // If this identifier was poisoned, and if it was not produced from a macro
585 // expansion, emit an error.
Ted Kremenek1a531572008-11-19 22:43:49 +0000586 if (II.isPoisoned() && CurPPLexer) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000587 HandlePoisonedIdentifier(Identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 }
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 // If this is a macro to be expanded, do it.
Chris Lattnercc1a8752007-10-07 08:44:20 +0000591 if (MacroInfo *MI = getMacroInfo(&II)) {
Abramo Bagnara163ada82012-01-01 22:01:04 +0000592 if (!DisableMacroExpansion) {
Richard Smith1fbf6fb2012-12-12 02:46:14 +0000593 if (!Identifier.isExpandDisabled() && MI->isEnabled()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 if (!HandleMacroExpandedIdentifier(Identifier, MI))
Douglas Gregor6be16fe2011-08-27 06:37:51 +0000595 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 } else {
597 // C99 6.10.3.4p2 says that a disabled macro may never again be
598 // expanded, even if it's in a context where it could be expanded in the
599 // future.
Chris Lattnerd2177732007-07-20 16:59:19 +0000600 Identifier.setFlag(Token::DisableExpand);
Richard Smith1fbf6fb2012-12-12 02:46:14 +0000601 if (MI->isObjectLike() || isNextPPTokenLParen())
602 Diag(Identifier, diag::pp_disabled_macro_expansion);
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 }
604 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 }
606
Richard Smith98d86b92011-10-11 19:57:52 +0000607 // If this identifier is a keyword in C++11, produce a warning. Don't warn if
608 // we're not considering macro expansion, since this identifier might be the
609 // name of a macro.
610 // FIXME: This warning is disabled in cases where it shouldn't be, like
611 // "#define constexpr constexpr", "int constexpr;"
612 if (II.isCXX11CompatKeyword() & !DisableMacroExpansion) {
613 Diag(Identifier, diag::warn_cxx11_keyword) << II.getName();
614 // Don't diagnose this keyword again in this translation unit.
615 II.setIsCXX11CompatKeyword(false);
616 }
617
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
619 // then we act as if it is the actual operator and not the textual
620 // representation of it.
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000621 if (II.isCPlusPlusOperatorKeyword())
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 Identifier.setIdentifierInfo(0);
623
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 // If this is an extension token, diagnose its use.
Steve Naroffb4eaf9c2008-09-02 18:50:17 +0000625 // We avoid diagnosing tokens that originate from macro definitions.
Eli Friedman2962f4d2009-04-28 03:59:15 +0000626 // FIXME: This warning is disabled in cases where it shouldn't be,
627 // like "#define TY typeof", "TY(1) x".
628 if (II.isExtensionToken() && !DisableMacroExpansion)
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 Diag(Identifier, diag::ext_token_used);
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000630
Douglas Gregor1b257af2012-12-11 22:11:52 +0000631 // If this is the 'import' contextual keyword, note
Ted Kremenek32ad2ee2012-03-01 22:07:04 +0000632 // that the next token indicates a module name.
Douglas Gregord6aba062012-01-04 06:20:15 +0000633 //
Douglas Gregor1b257af2012-12-11 22:11:52 +0000634 // Note that we do not treat 'import' as a contextual
Ted Kremenek32ad2ee2012-03-01 22:07:04 +0000635 // keyword when we're in a caching lexer, because caching lexers only get
636 // used in contexts where import declarations are disallowed.
637 if (II.isModulesImport() && !InMacroArgs && !DisableMacroExpansion &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000638 getLangOpts().Modules && CurLexerKind != CLK_CachingLexer) {
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000639 ModuleImportLoc = Identifier.getLocation();
Douglas Gregorb514c792011-11-30 04:26:53 +0000640 ModuleImportPath.clear();
641 ModuleImportExpectsIdentifier = true;
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000642 CurLexerKind = CLK_LexAfterModuleImport;
643 }
Douglas Gregor6aa52ec2011-08-26 23:56:07 +0000644}
645
Douglas Gregorc13a34b2012-01-03 19:32:59 +0000646/// \brief Lex a token following the 'import' contextual keyword.
Douglas Gregor5948ae12012-01-03 18:04:46 +0000647///
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000648void Preprocessor::LexAfterModuleImport(Token &Result) {
649 // Figure out what kind of lexer we actually have.
Douglas Gregord6aba062012-01-04 06:20:15 +0000650 recomputeCurLexerKind();
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000651
652 // Lex the next token.
653 Lex(Result);
654
Douglas Gregor6aa52ec2011-08-26 23:56:07 +0000655 // The token sequence
656 //
Douglas Gregor5948ae12012-01-03 18:04:46 +0000657 // import identifier (. identifier)*
658 //
Douglas Gregorc13a34b2012-01-03 19:32:59 +0000659 // indicates a module import directive. We already saw the 'import'
660 // contextual keyword, so now we're looking for the identifiers.
Douglas Gregorb514c792011-11-30 04:26:53 +0000661 if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) {
662 // We expected to see an identifier here, and we did; continue handling
663 // identifiers.
664 ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(),
665 Result.getLocation()));
666 ModuleImportExpectsIdentifier = false;
667 CurLexerKind = CLK_LexAfterModuleImport;
Douglas Gregor6aa52ec2011-08-26 23:56:07 +0000668 return;
Douglas Gregorb514c792011-11-30 04:26:53 +0000669 }
Douglas Gregor6aa52ec2011-08-26 23:56:07 +0000670
Douglas Gregorb514c792011-11-30 04:26:53 +0000671 // If we're expecting a '.' or a ';', and we got a '.', then wait until we
672 // see the next identifier.
673 if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) {
674 ModuleImportExpectsIdentifier = true;
675 CurLexerKind = CLK_LexAfterModuleImport;
676 return;
677 }
678
679 // If we have a non-empty module path, load the named module.
Argyrios Kyrtzidisf8afcff2012-09-29 01:06:10 +0000680 if (!ModuleImportPath.empty()) {
681 Module *Imported = TheModuleLoader.loadModule(ModuleImportLoc,
682 ModuleImportPath,
683 Module::MacrosVisible,
684 /*IsIncludeDirective=*/false);
685 if (Callbacks)
686 Callbacks->moduleImport(ModuleImportLoc, ModuleImportPath, Imported);
687 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000688}
Douglas Gregor2e222532009-07-02 17:08:52 +0000689
Andy Gibbs02a17682012-11-17 19:15:38 +0000690bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String,
Andy Gibbs97f84612012-11-17 19:16:52 +0000691 const char *DiagnosticTag,
Andy Gibbs02a17682012-11-17 19:15:38 +0000692 bool AllowMacroExpansion) {
693 // We need at least one string literal.
694 if (Result.isNot(tok::string_literal)) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000695 Diag(Result, diag::err_expected_string_literal)
696 << /*Source='in...'*/0 << DiagnosticTag;
Andy Gibbs02a17682012-11-17 19:15:38 +0000697 return false;
698 }
699
700 // Lex string literal tokens, optionally with macro expansion.
701 SmallVector<Token, 4> StrToks;
702 do {
703 StrToks.push_back(Result);
704
705 if (Result.hasUDSuffix())
706 Diag(Result, diag::err_invalid_string_udl);
707
708 if (AllowMacroExpansion)
709 Lex(Result);
710 else
711 LexUnexpandedToken(Result);
712 } while (Result.is(tok::string_literal));
713
714 // Concatenate and parse the strings.
715 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
716 assert(Literal.isAscii() && "Didn't allow wide strings in");
717
718 if (Literal.hadError)
719 return false;
720
721 if (Literal.Pascal) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000722 Diag(StrToks[0].getLocation(), diag::err_expected_string_literal)
723 << /*Source='in...'*/0 << DiagnosticTag;
Andy Gibbs02a17682012-11-17 19:15:38 +0000724 return false;
725 }
726
727 String = Literal.GetString();
728 return true;
729}
730
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000731void Preprocessor::addCommentHandler(CommentHandler *Handler) {
Douglas Gregor2e222532009-07-02 17:08:52 +0000732 assert(Handler && "NULL comment handler");
733 assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) ==
734 CommentHandlers.end() && "Comment handler already registered");
735 CommentHandlers.push_back(Handler);
736}
737
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000738void Preprocessor::removeCommentHandler(CommentHandler *Handler) {
Douglas Gregor2e222532009-07-02 17:08:52 +0000739 std::vector<CommentHandler *>::iterator Pos
740 = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler);
741 assert(Pos != CommentHandlers.end() && "Comment handler not registered");
742 CommentHandlers.erase(Pos);
743}
744
Chris Lattner046c2272010-01-18 22:35:47 +0000745bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
746 bool AnyPendingTokens = false;
Douglas Gregor2e222532009-07-02 17:08:52 +0000747 for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
748 HEnd = CommentHandlers.end();
Chris Lattner046c2272010-01-18 22:35:47 +0000749 H != HEnd; ++H) {
750 if ((*H)->HandleComment(*this, Comment))
751 AnyPendingTokens = true;
752 }
753 if (!AnyPendingTokens || getCommentRetentionState())
754 return false;
755 Lex(result);
756 return true;
Douglas Gregor2e222532009-07-02 17:08:52 +0000757}
758
Douglas Gregor6aa52ec2011-08-26 23:56:07 +0000759ModuleLoader::~ModuleLoader() { }
760
Douglas Gregor2e222532009-07-02 17:08:52 +0000761CommentHandler::~CommentHandler() { }
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000762
Douglas Gregorf44e8542010-08-24 19:08:16 +0000763CodeCompletionHandler::~CodeCompletionHandler() { }
764
Argyrios Kyrtzidis37ed1272012-12-04 07:27:05 +0000765void Preprocessor::createPreprocessingRecord() {
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000766 if (Record)
767 return;
768
Argyrios Kyrtzidis37ed1272012-12-04 07:27:05 +0000769 Record = new PreprocessingRecord(getSourceManager());
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000770 addPPCallbacks(Record);
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000771}