blob: 9ce4874f63ff1cb7c3395bef7ef5c58277d0489a [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,
Daniel Dunbar5814e652009-11-11 21:44:21 +000060 IdentifierInfoLookup* IILookup,
Douglas Gregor998b3d32011-09-01 23:39:15 +000061 bool OwnsHeaders,
Axel Naumanne55329d2012-03-16 10:40:17 +000062 bool DelayInitialization,
63 bool IncrProcessing)
Douglas Gregor36a16492012-10-24 17:46:57 +000064 : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), Target(target),
65 FileMgr(Headers.getFileMgr()),
Douglas Gregor6aa52ec2011-08-26 23:56:07 +000066 SourceMgr(SM), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader),
Axel Naumanne55329d2012-03-16 10:40:17 +000067 ExternalSource(0), Identifiers(opts, IILookup),
68 IncrementalProcessing(IncrProcessing), CodeComplete(0),
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000069 CodeCompletionFile(0), CodeCompletionOffset(0), CodeCompletionReached(0),
70 SkipMainFilePreamble(0, true), CurPPLexer(0),
Douglas Gregora8235d62012-10-09 23:05:51 +000071 CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), Listener(0),
72 MacroArgCache(0), Record(0), MIChainHead(0), MICache(0)
Douglas Gregor998b3d32011-09-01 23:39:15 +000073{
Daniel Dunbar5814e652009-11-11 21:44:21 +000074 OwnsHeaderSearch = OwnsHeaders;
Douglas Gregor998b3d32011-09-01 23:39:15 +000075
76 ScratchBuf = new ScratchBuffer(SourceMgr);
77 CounterValue = 0; // __COUNTER__ starts at 0.
78
79 // Clear stats.
80 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
81 NumIf = NumElse = NumEndif = 0;
82 NumEnteredSourceFiles = 0;
83 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
84 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
85 MaxIncludeStackDepth = 0;
86 NumSkipped = 0;
87
88 // Default to discarding comments.
89 KeepComments = false;
90 KeepMacroComments = false;
91 SuppressIncludeNotFoundError = false;
92
93 // Macro expansion is enabled.
94 DisableMacroExpansion = false;
David Blaikie8c0b3782012-06-06 18:52:13 +000095 MacroExpansionInDirectivesOverride = false;
Douglas Gregor998b3d32011-09-01 23:39:15 +000096 InMacroArgs = false;
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +000097 InMacroArgPreExpansion = false;
Douglas Gregor998b3d32011-09-01 23:39:15 +000098 NumCachedTokenLexers = 0;
Jordan Rose6fe6a492012-06-08 18:06:21 +000099 PragmasEnabled = true;
100
Douglas Gregor998b3d32011-09-01 23:39:15 +0000101 CachedLexPos = 0;
102
103 // We haven't read anything from the external source.
104 ReadMacrosFromExternalSource = false;
105
Douglas Gregor998b3d32011-09-01 23:39:15 +0000106 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
107 // This gets unpoisoned where it is allowed.
108 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
109 SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use);
110
111 // Initialize the pragma handlers.
112 PragmaHandlers = new PragmaNamespace(StringRef());
113 RegisterBuiltinPragmas();
114
115 // Initialize builtin macros like __LINE__ and friends.
116 RegisterBuiltinMacros();
117
David Blaikie4e4d0842012-03-11 07:00:24 +0000118 if(LangOpts.Borland) {
Douglas Gregor998b3d32011-09-01 23:39:15 +0000119 Ident__exception_info = getIdentifierInfo("_exception_info");
120 Ident___exception_info = getIdentifierInfo("__exception_info");
121 Ident_GetExceptionInfo = getIdentifierInfo("GetExceptionInformation");
122 Ident__exception_code = getIdentifierInfo("_exception_code");
123 Ident___exception_code = getIdentifierInfo("__exception_code");
124 Ident_GetExceptionCode = getIdentifierInfo("GetExceptionCode");
125 Ident__abnormal_termination = getIdentifierInfo("_abnormal_termination");
126 Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination");
127 Ident_AbnormalTermination = getIdentifierInfo("AbnormalTermination");
128 } else {
129 Ident__exception_info = Ident__exception_code = Ident__abnormal_termination = 0;
130 Ident___exception_info = Ident___exception_code = Ident___abnormal_termination = 0;
131 Ident_GetExceptionInfo = Ident_GetExceptionCode = Ident_AbnormalTermination = 0;
Douglas Gregordc58aa72012-01-30 06:01:29 +0000132 }
Argyrios Kyrtzidis25c25962012-06-02 18:08:09 +0000133
134 if (!DelayInitialization) {
135 assert(Target && "Must provide target information for PP initialization");
136 Initialize(*Target);
137 }
138}
139
140Preprocessor::~Preprocessor() {
141 assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
142
143 while (!IncludeMacroStack.empty()) {
144 delete IncludeMacroStack.back().TheLexer;
145 delete IncludeMacroStack.back().TheTokenLexer;
146 IncludeMacroStack.pop_back();
147 }
148
149 // Free any macro definitions.
150 for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next)
151 I->MI.Destroy();
152
153 // Free any cached macro expanders.
154 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
155 delete TokenLexerCache[i];
156
157 // Free any cached MacroArgs.
158 for (MacroArgs *ArgList = MacroArgCache; ArgList; )
159 ArgList = ArgList->deallocate();
160
161 // Release pragma information.
162 delete PragmaHandlers;
163
164 // Delete the scratch buffer info.
165 delete ScratchBuf;
166
167 // Delete the header search info, if we own it.
168 if (OwnsHeaderSearch)
169 delete &HeaderInfo;
170
171 delete Callbacks;
172}
173
174void Preprocessor::Initialize(const TargetInfo &Target) {
175 assert((!this->Target || this->Target == &Target) &&
176 "Invalid override of target information");
177 this->Target = &Target;
Douglas Gregordc58aa72012-01-30 06:01:29 +0000178
Argyrios Kyrtzidis25c25962012-06-02 18:08:09 +0000179 // Initialize information about built-ins.
180 BuiltinInfo.InitializeTarget(Target);
Douglas Gregordc58aa72012-01-30 06:01:29 +0000181 HeaderInfo.setTarget(Target);
Douglas Gregor998b3d32011-09-01 23:39:15 +0000182}
183
Ted Kremenek337edcd2009-02-12 03:26:59 +0000184void Preprocessor::setPTHManager(PTHManager* pm) {
185 PTH.reset(pm);
Douglas Gregor52e71082009-10-16 18:18:30 +0000186 FileMgr.addStatCache(PTH->createStatCache());
Ted Kremenek337edcd2009-02-12 03:26:59 +0000187}
188
Chris Lattnerd2177732007-07-20 16:59:19 +0000189void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000190 llvm::errs() << tok::getTokenName(Tok.getKind()) << " '"
191 << getSpelling(Tok) << "'";
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 if (!DumpFlags) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000195 llvm::errs() << "\t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 if (Tok.isAtStartOfLine())
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000197 llvm::errs() << " [StartOfLine]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 if (Tok.hasLeadingSpace())
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000199 llvm::errs() << " [LeadingSpace]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 if (Tok.isExpandDisabled())
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000201 llvm::errs() << " [ExpandDisabled]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 if (Tok.needsCleaning()) {
203 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000204 llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength())
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000205 << "']";
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000208 llvm::errs() << "\tLoc=<";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000209 DumpLocation(Tok.getLocation());
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000210 llvm::errs() << ">";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000211}
212
213void Preprocessor::DumpLocation(SourceLocation Loc) const {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000214 Loc.dump(SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000215}
216
217void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000218 llvm::errs() << "MACRO: ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
220 DumpToken(MI.getReplacementToken(i));
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000221 llvm::errs() << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 }
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000223 llvm::errs() << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000224}
225
226void Preprocessor::PrintStats() {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000227 llvm::errs() << "\n*** Preprocessor Stats:\n";
228 llvm::errs() << NumDirectives << " directives found:\n";
229 llvm::errs() << " " << NumDefined << " #define.\n";
230 llvm::errs() << " " << NumUndefined << " #undef.\n";
231 llvm::errs() << " #include/#include_next/#import:\n";
232 llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n";
233 llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n";
234 llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n";
235 llvm::errs() << " " << NumElse << " #else/#elif.\n";
236 llvm::errs() << " " << NumEndif << " #endif.\n";
237 llvm::errs() << " " << NumPragma << " #pragma.\n";
238 llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000239
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000240 llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000241 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
242 << NumFastMacroExpanded << " on the fast path.\n";
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000243 llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000244 << " token paste (##) operations performed, "
245 << NumFastTokenPaste << " on the fast path.\n";
Alexander Kornienkocd6df662012-08-13 10:46:42 +0000246
247 llvm::errs() << "\nPreprocessor Memory: " << getTotalMemory() << "B total";
248
249 llvm::errs() << "\n BumpPtr: " << BP.getTotalMemory();
250 llvm::errs() << "\n Macro Expanded Tokens: "
251 << llvm::capacity_in_bytes(MacroExpandedTokens);
252 llvm::errs() << "\n Predefines Buffer: " << Predefines.capacity();
253 llvm::errs() << "\n Macros: " << llvm::capacity_in_bytes(Macros);
254 llvm::errs() << "\n #pragma push_macro Info: "
255 << llvm::capacity_in_bytes(PragmaPushMacroInfo);
256 llvm::errs() << "\n Poison Reasons: "
257 << llvm::capacity_in_bytes(PoisonReasons);
258 llvm::errs() << "\n Comment Handlers: "
259 << llvm::capacity_in_bytes(CommentHandlers) << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000260}
261
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000262Preprocessor::macro_iterator
263Preprocessor::macro_begin(bool IncludeExternalMacros) const {
264 if (IncludeExternalMacros && ExternalSource &&
Douglas Gregor88a35862010-01-04 19:18:44 +0000265 !ReadMacrosFromExternalSource) {
266 ReadMacrosFromExternalSource = true;
267 ExternalSource->ReadDefinedMacros();
268 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000269
270 return Macros.begin();
Douglas Gregor88a35862010-01-04 19:18:44 +0000271}
272
Argyrios Kyrtzidisc5c5e922011-06-29 22:20:04 +0000273size_t Preprocessor::getTotalMemory() const {
Ted Kremenek91d1bd62011-07-26 21:17:24 +0000274 return BP.getTotalMemory()
Ted Kremenek67485092011-07-27 18:41:23 +0000275 + llvm::capacity_in_bytes(MacroExpandedTokens)
Ted Kremenek91d1bd62011-07-26 21:17:24 +0000276 + Predefines.capacity() /* Predefines buffer. */
Ted Kremenek67485092011-07-27 18:41:23 +0000277 + llvm::capacity_in_bytes(Macros)
278 + llvm::capacity_in_bytes(PragmaPushMacroInfo)
279 + llvm::capacity_in_bytes(PoisonReasons)
280 + llvm::capacity_in_bytes(CommentHandlers);
Argyrios Kyrtzidisc5c5e922011-06-29 22:20:04 +0000281}
282
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000283Preprocessor::macro_iterator
284Preprocessor::macro_end(bool IncludeExternalMacros) const {
285 if (IncludeExternalMacros && ExternalSource &&
Douglas Gregor88a35862010-01-04 19:18:44 +0000286 !ReadMacrosFromExternalSource) {
287 ReadMacrosFromExternalSource = true;
288 ExternalSource->ReadDefinedMacros();
289 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000290
291 return Macros.end();
Douglas Gregor88a35862010-01-04 19:18:44 +0000292}
293
Dmitri Gribenko19523542012-09-29 11:40:46 +0000294/// \brief Compares macro tokens with a specified token value sequence.
295static bool MacroDefinitionEquals(const MacroInfo *MI,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000296 ArrayRef<TokenValue> Tokens) {
Dmitri Gribenko19523542012-09-29 11:40:46 +0000297 return Tokens.size() == MI->getNumTokens() &&
298 std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin());
299}
300
301StringRef Preprocessor::getLastMacroWithSpelling(
302 SourceLocation Loc,
303 ArrayRef<TokenValue> Tokens) const {
304 SourceLocation BestLocation;
305 StringRef BestSpelling;
306 for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end();
307 I != E; ++I) {
308 if (!I->second->isObjectLike())
309 continue;
310 const MacroInfo *MI = I->second->findDefinitionAtLoc(Loc, SourceMgr);
311 if (!MI)
312 continue;
313 if (!MacroDefinitionEquals(MI, Tokens))
314 continue;
315 SourceLocation Location = I->second->getDefinitionLoc();
316 // Choose the macro defined latest.
317 if (BestLocation.isInvalid() ||
318 (Location.isValid() &&
319 SourceMgr.isBeforeInTranslationUnit(BestLocation, Location))) {
320 BestLocation = Location;
321 BestSpelling = I->first->getName();
322 }
323 }
324 return BestSpelling;
325}
326
Douglas Gregord6aba062012-01-04 06:20:15 +0000327void Preprocessor::recomputeCurLexerKind() {
328 if (CurLexer)
329 CurLexerKind = CLK_Lexer;
330 else if (CurPTHLexer)
331 CurLexerKind = CLK_PTHLexer;
332 else if (CurTokenLexer)
333 CurLexerKind = CLK_TokenLexer;
334 else
335 CurLexerKind = CLK_CachingLexer;
336}
337
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000338bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000339 unsigned CompleteLine,
340 unsigned CompleteColumn) {
341 assert(File);
342 assert(CompleteLine && CompleteColumn && "Starts from 1:1");
343 assert(!CodeCompletionFile && "Already set");
344
Douglas Gregor29684422009-12-02 06:49:09 +0000345 using llvm::MemoryBuffer;
346
Douglas Gregor29684422009-12-02 06:49:09 +0000347 // Load the actual file's contents.
Douglas Gregoraa38c3d2010-03-16 19:49:24 +0000348 bool Invalid = false;
349 const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
350 if (Invalid)
Douglas Gregor29684422009-12-02 06:49:09 +0000351 return true;
352
353 // Find the byte position of the truncation point.
354 const char *Position = Buffer->getBufferStart();
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000355 for (unsigned Line = 1; Line < CompleteLine; ++Line) {
Douglas Gregor29684422009-12-02 06:49:09 +0000356 for (; *Position; ++Position) {
357 if (*Position != '\r' && *Position != '\n')
358 continue;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000359
Douglas Gregor29684422009-12-02 06:49:09 +0000360 // Eat \r\n or \n\r as a single line.
361 if ((Position[1] == '\r' || Position[1] == '\n') &&
362 Position[0] != Position[1])
363 ++Position;
364 ++Position;
365 break;
366 }
367 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000368
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000369 Position += CompleteColumn - 1;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000370
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000371 // Insert '\0' at the code-completion point.
Douglas Gregorb760fe82009-12-08 21:45:46 +0000372 if (Position < Buffer->getBufferEnd()) {
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000373 CodeCompletionFile = File;
374 CodeCompletionOffset = Position - Buffer->getBufferStart();
375
376 MemoryBuffer *NewBuffer =
377 MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1,
378 Buffer->getBufferIdentifier());
Benjamin Kramer41a50a92011-09-04 20:26:28 +0000379 char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000380 char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
381 *NewPos = '\0';
382 std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
383 SourceMgr.overrideFileContents(File, NewBuffer);
Douglas Gregor29684422009-12-02 06:49:09 +0000384 }
385
386 return false;
387}
388
Douglas Gregor55817af2010-08-25 17:04:25 +0000389void Preprocessor::CodeCompleteNaturalLanguage() {
Douglas Gregor55817af2010-08-25 17:04:25 +0000390 if (CodeComplete)
391 CodeComplete->CodeCompleteNaturalLanguage();
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000392 setCodeCompletionReached();
Douglas Gregor55817af2010-08-25 17:04:25 +0000393}
394
Benjamin Kramer51f5fe32010-02-27 17:05:45 +0000395/// getSpelling - This method is used to get the spelling of a token into a
396/// SmallVector. Note that the returned StringRef may not point to the
397/// supplied buffer if a copy can be avoided.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000398StringRef Preprocessor::getSpelling(const Token &Tok,
399 SmallVectorImpl<char> &Buffer,
Douglas Gregor50f6af72010-03-16 05:20:39 +0000400 bool *Invalid) const {
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000401 // NOTE: this has to be checked *before* testing for an IdentifierInfo.
402 if (Tok.isNot(tok::raw_identifier)) {
403 // Try the fast path.
404 if (const IdentifierInfo *II = Tok.getIdentifierInfo())
405 return II->getName();
406 }
Benjamin Kramer51f5fe32010-02-27 17:05:45 +0000407
408 // Resize the buffer if we need to copy into it.
409 if (Tok.needsCleaning())
410 Buffer.resize(Tok.getLength());
411
412 const char *Ptr = Buffer.data();
Douglas Gregor50f6af72010-03-16 05:20:39 +0000413 unsigned Len = getSpelling(Tok, Ptr, Invalid);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000414 return StringRef(Ptr, Len);
Benjamin Kramer51f5fe32010-02-27 17:05:45 +0000415}
416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417/// CreateString - Plop the specified string into a scratch buffer and return a
418/// location for it. If specified, the source location provides a source
419/// location for the token.
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000420void Preprocessor::CreateString(StringRef Str, Token &Tok,
Abramo Bagnaraa08529c2011-10-03 18:39:03 +0000421 SourceLocation ExpansionLocStart,
422 SourceLocation ExpansionLocEnd) {
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000423 Tok.setLength(Str.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner47246be2009-01-26 19:29:26 +0000425 const char *DestPtr;
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000426 SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Abramo Bagnaraa08529c2011-10-03 18:39:03 +0000428 if (ExpansionLocStart.isValid())
429 Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart,
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000430 ExpansionLocEnd, Str.size());
Chris Lattner47246be2009-01-26 19:29:26 +0000431 Tok.setLocation(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000433 // If this is a raw identifier or a literal token, set the pointer data.
434 if (Tok.is(tok::raw_identifier))
435 Tok.setRawIdentifierData(DestPtr);
436 else if (Tok.isLiteral())
Chris Lattner47246be2009-01-26 19:29:26 +0000437 Tok.setLiteralData(DestPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000438}
439
Douglas Gregor90db2602011-12-02 01:47:07 +0000440Module *Preprocessor::getCurrentModule() {
David Blaikie4e4d0842012-03-11 07:00:24 +0000441 if (getLangOpts().CurrentModule.empty())
Douglas Gregor90db2602011-12-02 01:47:07 +0000442 return 0;
443
David Blaikie4e4d0842012-03-11 07:00:24 +0000444 return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule);
Douglas Gregor90db2602011-12-02 01:47:07 +0000445}
Chris Lattner97ba77c2007-07-16 06:48:38 +0000446
Chris Lattner53b0dab2007-10-09 22:10:18 +0000447//===----------------------------------------------------------------------===//
448// Preprocessor Initialization Methods
449//===----------------------------------------------------------------------===//
450
Chris Lattner53b0dab2007-10-09 22:10:18 +0000451
452/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begeman6b616022008-01-07 04:01:26 +0000453/// which implicitly adds the builtin defines etc.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000454void Preprocessor::EnterMainSourceFile() {
Chris Lattner05db4272009-02-13 19:33:24 +0000455 // We do not allow the preprocessor to reenter the main file. Doing so will
456 // cause FileID's to accumulate information from both runs (e.g. #line
457 // information) and predefined macros aren't guaranteed to be set properly.
458 assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
Chris Lattner2b2453a2009-01-17 06:22:33 +0000459 FileID MainFileID = SourceMgr.getMainFileID();
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000461 // If MainFileID is loaded it means we loaded an AST file, no need to enter
462 // a main file.
463 if (!SourceMgr.isLoadedFileID(MainFileID)) {
464 // Enter the main file source buffer.
465 EnterSourceFile(MainFileID, 0, SourceLocation());
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000466
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000467 // If we've been asked to skip bytes in the main file (e.g., as part of a
468 // precompiled preamble), do so now.
469 if (SkipMainFilePreamble.first > 0)
470 CurLexer->SkipBytes(SkipMainFilePreamble.first,
471 SkipMainFilePreamble.second);
472
473 // Tell the header info that the main file was entered. If the file is later
474 // #imported, it won't be re-entered.
475 if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
476 HeaderInfo.IncrementIncludeCount(FE);
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Benjamin Kramerffd6e392009-12-31 15:33:09 +0000479 // Preprocess Predefines to populate the initial preprocessor state.
Mike Stump1eb44332009-09-09 15:08:12 +0000480 llvm::MemoryBuffer *SB =
Chris Lattnera0a270c2010-04-05 22:42:27 +0000481 llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
Douglas Gregor043266b2010-08-26 14:07:34 +0000482 assert(SB && "Cannot create predefined source buffer");
Meador Inge9416d422012-06-19 18:17:30 +0000483 FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000484 assert(!FID.isInvalid() && "Could not create FileID for predefines?");
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner53b0dab2007-10-09 22:10:18 +0000486 // Start parsing the predefines.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000487 EnterSourceFile(FID, 0, SourceLocation());
Chris Lattner53b0dab2007-10-09 22:10:18 +0000488}
Chris Lattner97ba77c2007-07-16 06:48:38 +0000489
Daniel Dunbardbd82092010-03-23 05:09:10 +0000490void Preprocessor::EndSourceFile() {
491 // Notify the client that we reached the end of the source file.
492 if (Callbacks)
493 Callbacks->EndOfMainFile();
494}
Reid Spencer5f016e22007-07-11 17:01:13 +0000495
496//===----------------------------------------------------------------------===//
497// Lexer Event Handling.
498//===----------------------------------------------------------------------===//
499
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000500/// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
501/// identifier information for the token and install it into the token,
502/// updating the token kind accordingly.
503IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
504 assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!");
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 // Look up this token, see if it is a macro, or if it is a language keyword.
507 IdentifierInfo *II;
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000508 if (!Identifier.needsCleaning()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 // No cleaning needed, just use the characters from the lexed buffer.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000510 II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(),
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000511 Identifier.getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 } else {
513 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000514 SmallString<64> IdentifierBuffer;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000515 StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
Benjamin Kramerddeea562010-02-27 13:44:12 +0000516 II = getIdentifierInfo(CleanedStr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 }
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000518
519 // Update the token info (identifier info and appropriate token kind).
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 Identifier.setIdentifierInfo(II);
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000521 Identifier.setKind(II->getTokenID());
522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 return II;
524}
525
John Wiegley28bbe4b2011-04-28 01:08:34 +0000526void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) {
527 PoisonReasons[II] = DiagID;
528}
529
530void Preprocessor::PoisonSEHIdentifiers(bool Poison) {
531 assert(Ident__exception_code && Ident__exception_info);
532 assert(Ident___exception_code && Ident___exception_info);
533 Ident__exception_code->setIsPoisoned(Poison);
534 Ident___exception_code->setIsPoisoned(Poison);
535 Ident_GetExceptionCode->setIsPoisoned(Poison);
536 Ident__exception_info->setIsPoisoned(Poison);
537 Ident___exception_info->setIsPoisoned(Poison);
538 Ident_GetExceptionInfo->setIsPoisoned(Poison);
539 Ident__abnormal_termination->setIsPoisoned(Poison);
540 Ident___abnormal_termination->setIsPoisoned(Poison);
541 Ident_AbnormalTermination->setIsPoisoned(Poison);
542}
543
544void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) {
545 assert(Identifier.getIdentifierInfo() &&
546 "Can't handle identifiers without identifier info!");
547 llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it =
548 PoisonReasons.find(Identifier.getIdentifierInfo());
549 if(it == PoisonReasons.end())
550 Diag(Identifier, diag::err_pp_used_poisoned_id);
551 else
552 Diag(Identifier,it->second) << Identifier.getIdentifierInfo();
553}
Reid Spencer5f016e22007-07-11 17:01:13 +0000554
555/// HandleIdentifier - This callback is invoked when the lexer reads an
556/// identifier. This callback looks up the identifier in the map and/or
557/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattner6a170eb2009-01-21 07:43:11 +0000558///
559/// Note that callers of this method are guarded by checking the
560/// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the
561/// IdentifierInfo methods that compute these properties will need to change to
562/// match.
Chris Lattnerd2177732007-07-20 16:59:19 +0000563void Preprocessor::HandleIdentifier(Token &Identifier) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 assert(Identifier.getIdentifierInfo() &&
565 "Can't handle identifiers without identifier info!");
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 IdentifierInfo &II = *Identifier.getIdentifierInfo();
568
Douglas Gregoreee242f2011-10-27 09:33:13 +0000569 // If the information about this identifier is out of date, update it from
570 // the external source.
Douglas Gregor193f91b2012-06-29 18:27:59 +0000571 // We have to treat __VA_ARGS__ in a special way, since it gets
572 // serialized with isPoisoned = true, but our preprocessor may have
573 // unpoisoned it if we're defining a C99 macro.
Douglas Gregoreee242f2011-10-27 09:33:13 +0000574 if (II.isOutOfDate()) {
Douglas Gregor193f91b2012-06-29 18:27:59 +0000575 bool CurrentIsPoisoned = false;
576 if (&II == Ident__VA_ARGS__)
577 CurrentIsPoisoned = Ident__VA_ARGS__->isPoisoned();
578
Douglas Gregoreee242f2011-10-27 09:33:13 +0000579 ExternalSource->updateOutOfDateIdentifier(II);
580 Identifier.setKind(II.getTokenID());
Douglas Gregor193f91b2012-06-29 18:27:59 +0000581
582 if (&II == Ident__VA_ARGS__)
583 II.setIsPoisoned(CurrentIsPoisoned);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000584 }
585
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 // If this identifier was poisoned, and if it was not produced from a macro
587 // expansion, emit an error.
Ted Kremenek1a531572008-11-19 22:43:49 +0000588 if (II.isPoisoned() && CurPPLexer) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000589 HandlePoisonedIdentifier(Identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 }
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 // If this is a macro to be expanded, do it.
Chris Lattnercc1a8752007-10-07 08:44:20 +0000593 if (MacroInfo *MI = getMacroInfo(&II)) {
Abramo Bagnara163ada82012-01-01 22:01:04 +0000594 if (!DisableMacroExpansion) {
Richard Smith1fbf6fb2012-12-12 02:46:14 +0000595 if (!Identifier.isExpandDisabled() && MI->isEnabled()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 if (!HandleMacroExpandedIdentifier(Identifier, MI))
Douglas Gregor6be16fe2011-08-27 06:37:51 +0000597 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 } else {
599 // C99 6.10.3.4p2 says that a disabled macro may never again be
600 // expanded, even if it's in a context where it could be expanded in the
601 // future.
Chris Lattnerd2177732007-07-20 16:59:19 +0000602 Identifier.setFlag(Token::DisableExpand);
Richard Smith1fbf6fb2012-12-12 02:46:14 +0000603 if (MI->isObjectLike() || isNextPPTokenLParen())
604 Diag(Identifier, diag::pp_disabled_macro_expansion);
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 }
606 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 }
608
Richard Smith98d86b92011-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Jahanianafbc6812010-09-03 17:33:04 +0000623 if (II.isCPlusPlusOperatorKeyword())
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 Identifier.setIdentifierInfo(0);
625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 // If this is an extension token, diagnose its use.
Steve Naroffb4eaf9c2008-09-02 18:50:17 +0000627 // We avoid diagnosing tokens that originate from macro definitions.
Eli Friedman2962f4d2009-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)
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 Diag(Identifier, diag::ext_token_used);
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000632
Douglas Gregor1b257af2012-12-11 22:11:52 +0000633 // If this is the 'import' contextual keyword, note
Ted Kremenek32ad2ee2012-03-01 22:07:04 +0000634 // that the next token indicates a module name.
Douglas Gregord6aba062012-01-04 06:20:15 +0000635 //
Douglas Gregor1b257af2012-12-11 22:11:52 +0000636 // Note that we do not treat 'import' as a contextual
Ted Kremenek32ad2ee2012-03-01 22:07:04 +0000637 // 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 Blaikie4e4d0842012-03-11 07:00:24 +0000640 getLangOpts().Modules && CurLexerKind != CLK_CachingLexer) {
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000641 ModuleImportLoc = Identifier.getLocation();
Douglas Gregorb514c792011-11-30 04:26:53 +0000642 ModuleImportPath.clear();
643 ModuleImportExpectsIdentifier = true;
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000644 CurLexerKind = CLK_LexAfterModuleImport;
645 }
Douglas Gregor6aa52ec2011-08-26 23:56:07 +0000646}
647
Douglas Gregorc13a34b2012-01-03 19:32:59 +0000648/// \brief Lex a token following the 'import' contextual keyword.
Douglas Gregor5948ae12012-01-03 18:04:46 +0000649///
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000650void Preprocessor::LexAfterModuleImport(Token &Result) {
651 // Figure out what kind of lexer we actually have.
Douglas Gregord6aba062012-01-04 06:20:15 +0000652 recomputeCurLexerKind();
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000653
654 // Lex the next token.
655 Lex(Result);
656
Douglas Gregor6aa52ec2011-08-26 23:56:07 +0000657 // The token sequence
658 //
Douglas Gregor5948ae12012-01-03 18:04:46 +0000659 // import identifier (. identifier)*
660 //
Douglas Gregorc13a34b2012-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 Gregorb514c792011-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 Gregor6aa52ec2011-08-26 23:56:07 +0000670 return;
Douglas Gregorb514c792011-11-30 04:26:53 +0000671 }
Douglas Gregor6aa52ec2011-08-26 23:56:07 +0000672
Douglas Gregorb514c792011-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 Kyrtzidisf8afcff2012-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000690}
Douglas Gregor2e222532009-07-02 17:08:52 +0000691
Andy Gibbs02a17682012-11-17 19:15:38 +0000692bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String,
Andy Gibbs97f84612012-11-17 19:16:52 +0000693 const char *DiagnosticTag,
Andy Gibbs02a17682012-11-17 19:15:38 +0000694 bool AllowMacroExpansion) {
695 // We need at least one string literal.
696 if (Result.isNot(tok::string_literal)) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000697 Diag(Result, diag::err_expected_string_literal)
698 << /*Source='in...'*/0 << DiagnosticTag;
Andy Gibbs02a17682012-11-17 19:15:38 +0000699 return false;
700 }
701
702 // Lex string literal tokens, optionally with macro expansion.
703 SmallVector<Token, 4> StrToks;
704 do {
705 StrToks.push_back(Result);
706
707 if (Result.hasUDSuffix())
708 Diag(Result, diag::err_invalid_string_udl);
709
710 if (AllowMacroExpansion)
711 Lex(Result);
712 else
713 LexUnexpandedToken(Result);
714 } while (Result.is(tok::string_literal));
715
716 // Concatenate and parse the strings.
717 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
718 assert(Literal.isAscii() && "Didn't allow wide strings in");
719
720 if (Literal.hadError)
721 return false;
722
723 if (Literal.Pascal) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000724 Diag(StrToks[0].getLocation(), diag::err_expected_string_literal)
725 << /*Source='in...'*/0 << DiagnosticTag;
Andy Gibbs02a17682012-11-17 19:15:38 +0000726 return false;
727 }
728
729 String = Literal.GetString();
730 return true;
731}
732
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000733void Preprocessor::addCommentHandler(CommentHandler *Handler) {
Douglas Gregor2e222532009-07-02 17:08:52 +0000734 assert(Handler && "NULL comment handler");
735 assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) ==
736 CommentHandlers.end() && "Comment handler already registered");
737 CommentHandlers.push_back(Handler);
738}
739
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000740void Preprocessor::removeCommentHandler(CommentHandler *Handler) {
Douglas Gregor2e222532009-07-02 17:08:52 +0000741 std::vector<CommentHandler *>::iterator Pos
742 = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler);
743 assert(Pos != CommentHandlers.end() && "Comment handler not registered");
744 CommentHandlers.erase(Pos);
745}
746
Chris Lattner046c2272010-01-18 22:35:47 +0000747bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
748 bool AnyPendingTokens = false;
Douglas Gregor2e222532009-07-02 17:08:52 +0000749 for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
750 HEnd = CommentHandlers.end();
Chris Lattner046c2272010-01-18 22:35:47 +0000751 H != HEnd; ++H) {
752 if ((*H)->HandleComment(*this, Comment))
753 AnyPendingTokens = true;
754 }
755 if (!AnyPendingTokens || getCommentRetentionState())
756 return false;
757 Lex(result);
758 return true;
Douglas Gregor2e222532009-07-02 17:08:52 +0000759}
760
Douglas Gregor6aa52ec2011-08-26 23:56:07 +0000761ModuleLoader::~ModuleLoader() { }
762
Douglas Gregor2e222532009-07-02 17:08:52 +0000763CommentHandler::~CommentHandler() { }
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000764
Douglas Gregorf44e8542010-08-24 19:08:16 +0000765CodeCompletionHandler::~CodeCompletionHandler() { }
766
Argyrios Kyrtzidis37ed1272012-12-04 07:27:05 +0000767void Preprocessor::createPreprocessingRecord() {
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000768 if (Record)
769 return;
770
Argyrios Kyrtzidis37ed1272012-12-04 07:27:05 +0000771 Record = new PreprocessingRecord(getSourceManager());
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000772 addPPCallbacks(Record);
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000773}