1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Implements # directive processing for the Preprocessor.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Lex/CodeCompletionHandler.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/HeaderSearchOptions.h"
21 #include "clang/Lex/LexDiagnostic.h"
22 #include "clang/Lex/LiteralSupport.h"
23 #include "clang/Lex/MacroInfo.h"
24 #include "clang/Lex/ModuleLoader.h"
25 #include "clang/Lex/Pragma.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/SaveAndRestore.h"
30 using namespace clang;
32 //===----------------------------------------------------------------------===//
33 // Utility Methods for Preprocessor Directive Handling.
34 //===----------------------------------------------------------------------===//
36 MacroInfo *Preprocessor::AllocateMacroInfo() {
37 MacroInfoChain *MIChain;
41 MICache = MICache->Next;
44 MIChain = BP.Allocate<MacroInfoChain>();
47 MIChain->Next = MIChainHead;
48 MIChain->Prev = nullptr;
50 MIChainHead->Prev = MIChain;
51 MIChainHead = MIChain;
53 return &(MIChain->MI);
56 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
57 MacroInfo *MI = AllocateMacroInfo();
58 new (MI) MacroInfo(L);
62 MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
63 unsigned SubModuleID) {
64 static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
65 "alignment for MacroInfo is less than the ID");
66 DeserializedMacroInfoChain *MIChain =
67 BP.Allocate<DeserializedMacroInfoChain>();
68 MIChain->Next = DeserialMIChainHead;
69 DeserialMIChainHead = MIChain;
71 MacroInfo *MI = &MIChain->MI;
72 new (MI) MacroInfo(L);
73 MI->FromASTFile = true;
74 MI->setOwningModuleID(SubModuleID);
79 Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc,
81 DefMacroDirective *MD = BP.Allocate<DefMacroDirective>();
82 new (MD) DefMacroDirective(MI, Loc, isImported);
87 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
88 UndefMacroDirective *MD = BP.Allocate<UndefMacroDirective>();
89 new (MD) UndefMacroDirective(UndefLoc);
93 VisibilityMacroDirective *
94 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
96 VisibilityMacroDirective *MD = BP.Allocate<VisibilityMacroDirective>();
97 new (MD) VisibilityMacroDirective(Loc, isPublic);
101 /// \brief Release the specified MacroInfo to be reused for allocating
102 /// new MacroInfo objects.
103 void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) {
104 MacroInfoChain *MIChain = (MacroInfoChain *)MI;
105 if (MacroInfoChain *Prev = MIChain->Prev) {
106 MacroInfoChain *Next = MIChain->Next;
111 assert(MIChainHead == MIChain);
112 MIChainHead = MIChain->Next;
113 MIChainHead->Prev = nullptr;
115 MIChain->Next = MICache;
121 /// \brief Read and discard all tokens remaining on the current line until
122 /// the tok::eod token is found.
123 void Preprocessor::DiscardUntilEndOfDirective() {
126 LexUnexpandedToken(Tmp);
127 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
128 } while (Tmp.isNot(tok::eod));
131 bool Preprocessor::CheckMacroName(Token &MacroNameTok, char isDefineUndef) {
132 // Missing macro name?
133 if (MacroNameTok.is(tok::eod))
134 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
136 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
138 bool Invalid = false;
139 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
141 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
142 II = getIdentifierInfo(Spelling);
144 if (!II->isCPlusPlusOperatorKeyword())
145 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
147 // C++ 2.5p2: Alternative tokens behave the same as its primary token
148 // except for their spellings.
149 Diag(MacroNameTok, getLangOpts().MicrosoftExt
150 ? diag::ext_pp_operator_used_as_macro_name
151 : diag::err_pp_operator_used_as_macro_name)
152 << II << MacroNameTok.getKind();
154 // Allow #defining |and| and friends for Microsoft compatibility or
155 // recovery when legacy C headers are included in C++.
156 MacroNameTok.setIdentifierInfo(II);
159 if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
160 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
161 return Diag(MacroNameTok, diag::err_defined_macro_name);
164 if (isDefineUndef == 2 && II->hasMacroDefinition() &&
165 getMacroInfo(II)->isBuiltinMacro()) {
166 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
167 // and C++ [cpp.predefined]p4], but allow it as an extension.
168 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
171 // Okay, we got a good identifier.
175 /// \brief Lex and validate a macro name, which occurs after a
176 /// \#define or \#undef.
178 /// This sets the token kind to eod and discards the rest
179 /// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if
180 /// this is due to a a \#define, 2 if \#undef directive, 0 if it is something
181 /// else (e.g. \#ifdef).
182 void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
183 // Read the token, don't allow macro expansion on it.
184 LexUnexpandedToken(MacroNameTok);
186 if (MacroNameTok.is(tok::code_completion)) {
188 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
189 setCodeCompletionReached();
190 LexUnexpandedToken(MacroNameTok);
193 if (!CheckMacroName(MacroNameTok, isDefineUndef))
196 // Invalid macro name, read and discard the rest of the line and set the
197 // token kind to tok::eod if necessary.
198 if (MacroNameTok.isNot(tok::eod)) {
199 MacroNameTok.setKind(tok::eod);
200 DiscardUntilEndOfDirective();
204 /// \brief Ensure that the next token is a tok::eod token.
206 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
207 /// true, then we consider macros that expand to zero tokens as being ok.
208 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
210 // Lex unexpanded tokens for most directives: macros might expand to zero
211 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
212 // #line) allow empty macros.
216 LexUnexpandedToken(Tmp);
218 // There should be no tokens after the directive, but we allow them as an
220 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
221 LexUnexpandedToken(Tmp);
223 if (Tmp.isNot(tok::eod)) {
224 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
225 // or if this is a macro-style preprocessing directive, because it is more
226 // trouble than it is worth to insert /**/ and check that there is no /**/
227 // in the range also.
229 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
231 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
232 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
233 DiscardUntilEndOfDirective();
239 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
240 /// decided that the subsequent tokens are in the \#if'd out portion of the
241 /// file. Lex the rest of the file, until we see an \#endif. If
242 /// FoundNonSkipPortion is true, then we have already emitted code for part of
243 /// this \#if directive, so \#else/\#elif blocks should never be entered.
244 /// If ElseOk is true, then \#else directives are ok, if not, then we have
245 /// already seen one so a \#else directive is a duplicate. When this returns,
246 /// the caller can lex the first valid token.
247 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
248 bool FoundNonSkipPortion,
250 SourceLocation ElseLoc) {
252 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
254 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
255 FoundNonSkipPortion, FoundElse);
258 PTHSkipExcludedConditionalBlock();
262 // Enter raw mode to disable identifier lookup (and thus macro expansion),
263 // disabling warnings, etc.
264 CurPPLexer->LexingRawMode = true;
269 if (Tok.is(tok::code_completion)) {
271 CodeComplete->CodeCompleteInConditionalExclusion();
272 setCodeCompletionReached();
276 // If this is the end of the buffer, we have an error.
277 if (Tok.is(tok::eof)) {
278 // Emit errors for each unterminated conditional on the stack, including
280 while (!CurPPLexer->ConditionalStack.empty()) {
281 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
282 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
283 diag::err_pp_unterminated_conditional);
284 CurPPLexer->ConditionalStack.pop_back();
287 // Just return and let the caller lex after this #include.
291 // If this token is not a preprocessor directive, just skip it.
292 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
295 // We just parsed a # character at the start of a line, so we're in
296 // directive mode. Tell the lexer this so any newlines we see will be
297 // converted into an EOD token (this terminates the macro).
298 CurPPLexer->ParsingPreprocessorDirective = true;
299 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
302 // Read the next token, the directive flavor.
303 LexUnexpandedToken(Tok);
305 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
306 // something bogus), skip it.
307 if (Tok.isNot(tok::raw_identifier)) {
308 CurPPLexer->ParsingPreprocessorDirective = false;
309 // Restore comment saving mode.
310 if (CurLexer) CurLexer->resetExtendedTokenMode();
314 // If the first letter isn't i or e, it isn't intesting to us. We know that
315 // this is safe in the face of spelling differences, because there is no way
316 // to spell an i/e in a strange way that is another letter. Skipping this
317 // allows us to avoid looking up the identifier info for #define/#undef and
318 // other common directives.
319 StringRef RI = Tok.getRawIdentifier();
321 char FirstChar = RI[0];
322 if (FirstChar >= 'a' && FirstChar <= 'z' &&
323 FirstChar != 'i' && FirstChar != 'e') {
324 CurPPLexer->ParsingPreprocessorDirective = false;
325 // Restore comment saving mode.
326 if (CurLexer) CurLexer->resetExtendedTokenMode();
330 // Get the identifier name without trigraphs or embedded newlines. Note
331 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
333 char DirectiveBuf[20];
335 if (!Tok.needsCleaning() && RI.size() < 20) {
338 std::string DirectiveStr = getSpelling(Tok);
339 unsigned IdLen = DirectiveStr.size();
341 CurPPLexer->ParsingPreprocessorDirective = false;
342 // Restore comment saving mode.
343 if (CurLexer) CurLexer->resetExtendedTokenMode();
346 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
347 Directive = StringRef(DirectiveBuf, IdLen);
350 if (Directive.startswith("if")) {
351 StringRef Sub = Directive.substr(2);
352 if (Sub.empty() || // "if"
353 Sub == "def" || // "ifdef"
354 Sub == "ndef") { // "ifndef"
355 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
356 // bother parsing the condition.
357 DiscardUntilEndOfDirective();
358 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
359 /*foundnonskip*/false,
362 } else if (Directive[0] == 'e') {
363 StringRef Sub = Directive.substr(1);
364 if (Sub == "ndif") { // "endif"
365 PPConditionalInfo CondInfo;
366 CondInfo.WasSkipping = true; // Silence bogus warning.
367 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
368 (void)InCond; // Silence warning in no-asserts mode.
369 assert(!InCond && "Can't be skipping if not in a conditional!");
371 // If we popped the outermost skipping block, we're done skipping!
372 if (!CondInfo.WasSkipping) {
373 // Restore the value of LexingRawMode so that trailing comments
374 // are handled correctly, if we've reached the outermost block.
375 CurPPLexer->LexingRawMode = false;
376 CheckEndOfDirective("endif");
377 CurPPLexer->LexingRawMode = true;
379 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
382 DiscardUntilEndOfDirective();
384 } else if (Sub == "lse") { // "else".
385 // #else directive in a skipping conditional. If not in some other
386 // skipping conditional, and if #else hasn't already been seen, enter it
387 // as a non-skipping conditional.
388 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
390 // If this is a #else with a #else before it, report the error.
391 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
393 // Note that we've seen a #else in this conditional.
394 CondInfo.FoundElse = true;
396 // If the conditional is at the top level, and the #if block wasn't
397 // entered, enter the #else block now.
398 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
399 CondInfo.FoundNonSkip = true;
400 // Restore the value of LexingRawMode so that trailing comments
401 // are handled correctly.
402 CurPPLexer->LexingRawMode = false;
403 CheckEndOfDirective("else");
404 CurPPLexer->LexingRawMode = true;
406 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
409 DiscardUntilEndOfDirective(); // C99 6.10p4.
411 } else if (Sub == "lif") { // "elif".
412 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
414 // If this is a #elif with a #else before it, report the error.
415 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
417 // If this is in a skipping block or if we're already handled this #if
418 // block, don't bother parsing the condition.
419 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
420 DiscardUntilEndOfDirective();
422 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
423 // Restore the value of LexingRawMode so that identifiers are
424 // looked up, etc, inside the #elif expression.
425 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
426 CurPPLexer->LexingRawMode = false;
427 IdentifierInfo *IfNDefMacro = nullptr;
428 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
429 CurPPLexer->LexingRawMode = true;
431 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
432 Callbacks->Elif(Tok.getLocation(),
433 SourceRange(CondBegin, CondEnd),
434 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
436 // If this condition is true, enter it!
438 CondInfo.FoundNonSkip = true;
445 CurPPLexer->ParsingPreprocessorDirective = false;
446 // Restore comment saving mode.
447 if (CurLexer) CurLexer->resetExtendedTokenMode();
450 // Finally, if we are out of the conditional (saw an #endif or ran off the end
451 // of the file, just stop skipping and return to lexing whatever came after
453 CurPPLexer->LexingRawMode = false;
456 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
457 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
461 void Preprocessor::PTHSkipExcludedConditionalBlock() {
465 assert(CurPTHLexer->LexingRawMode == false);
467 // Skip to the next '#else', '#elif', or #endif.
468 if (CurPTHLexer->SkipBlock()) {
469 // We have reached an #endif. Both the '#' and 'endif' tokens
470 // have been consumed by the PTHLexer. Just pop off the condition level.
471 PPConditionalInfo CondInfo;
472 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
473 (void)InCond; // Silence warning in no-asserts mode.
474 assert(!InCond && "Can't be skipping if not in a conditional!");
478 // We have reached a '#else' or '#elif'. Lex the next token to get
479 // the directive flavor.
481 LexUnexpandedToken(Tok);
483 // We can actually look up the IdentifierInfo here since we aren't in
485 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
487 if (K == tok::pp_else) {
488 // #else: Enter the else condition. We aren't in a nested condition
489 // since we skip those. We're always in the one matching the last
490 // blocked we skipped.
491 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
492 // Note that we've seen a #else in this conditional.
493 CondInfo.FoundElse = true;
495 // If the #if block wasn't entered then enter the #else block now.
496 if (!CondInfo.FoundNonSkip) {
497 CondInfo.FoundNonSkip = true;
499 // Scan until the eod token.
500 CurPTHLexer->ParsingPreprocessorDirective = true;
501 DiscardUntilEndOfDirective();
502 CurPTHLexer->ParsingPreprocessorDirective = false;
507 // Otherwise skip this block.
511 assert(K == tok::pp_elif);
512 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
514 // If this is a #elif with a #else before it, report the error.
515 if (CondInfo.FoundElse)
516 Diag(Tok, diag::pp_err_elif_after_else);
518 // If this is in a skipping block or if we're already handled this #if
519 // block, don't bother parsing the condition. We just skip this block.
520 if (CondInfo.FoundNonSkip)
523 // Evaluate the condition of the #elif.
524 IdentifierInfo *IfNDefMacro = nullptr;
525 CurPTHLexer->ParsingPreprocessorDirective = true;
526 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
527 CurPTHLexer->ParsingPreprocessorDirective = false;
529 // If this condition is true, enter it!
531 CondInfo.FoundNonSkip = true;
535 // Otherwise, skip this block and go to the next one.
540 Module *Preprocessor::getModuleForLocation(SourceLocation FilenameLoc) {
541 ModuleMap &ModMap = HeaderInfo.getModuleMap();
542 if (SourceMgr.isInMainFile(FilenameLoc)) {
543 if (Module *CurMod = getCurrentModule())
544 return CurMod; // Compiling a module.
545 return HeaderInfo.getModuleMap().SourceModule; // Compiling a source.
547 // Try to determine the module of the include directive.
548 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
549 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(FilenameLoc));
550 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
551 // The include comes from a file.
552 return ModMap.findModuleForHeader(EntryOfIncl).getModule();
554 // The include does not come from a file,
555 // so it is probably a module compilation.
556 return getCurrentModule();
560 const FileEntry *Preprocessor::LookupFile(
561 SourceLocation FilenameLoc,
564 const DirectoryLookup *FromDir,
565 const DirectoryLookup *&CurDir,
566 SmallVectorImpl<char> *SearchPath,
567 SmallVectorImpl<char> *RelativePath,
568 ModuleMap::KnownHeader *SuggestedModule,
570 // If the header lookup mechanism may be relative to the current inclusion
571 // stack, record the parent #includes.
572 SmallVector<const FileEntry *, 16> Includers;
574 FileID FID = getCurrentFileLexer()->getFileID();
575 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
577 // If there is no file entry associated with this file, it must be the
578 // predefines buffer. Any other file is not lexed with a normal lexer, so
579 // it won't be scanned for preprocessor directives. If we have the
580 // predefines buffer, resolve #include references (which come from the
581 // -include command line argument) as if they came from the main file, this
582 // affects file lookup etc.
584 FileEnt = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
587 Includers.push_back(FileEnt);
589 // MSVC searches the current include stack from top to bottom for
590 // headers included by quoted include directives.
591 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
592 if (LangOpts.MSVCCompat && !isAngled) {
593 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
594 IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
595 if (IsFileLexer(ISEntry))
596 if ((FileEnt = SourceMgr.getFileEntryForID(
597 ISEntry.ThePPLexer->getFileID())))
598 Includers.push_back(FileEnt);
603 // Do a standard file entry lookup.
604 CurDir = CurDirLookup;
605 const FileEntry *FE = HeaderInfo.LookupFile(
606 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
607 RelativePath, SuggestedModule, SkipCache);
609 if (SuggestedModule && !LangOpts.AsmPreprocessor)
610 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
611 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
615 const FileEntry *CurFileEnt;
616 // Otherwise, see if this is a subframework header. If so, this is relative
617 // to one of the headers on the #include stack. Walk the list of the current
618 // headers on the #include stack and pass them to HeaderInfo.
620 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) {
621 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
622 SearchPath, RelativePath,
624 if (SuggestedModule && !LangOpts.AsmPreprocessor)
625 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
626 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
632 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
633 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
634 if (IsFileLexer(ISEntry)) {
636 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) {
637 if ((FE = HeaderInfo.LookupSubframeworkHeader(
638 Filename, CurFileEnt, SearchPath, RelativePath,
640 if (SuggestedModule && !LangOpts.AsmPreprocessor)
641 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
642 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
649 // Otherwise, we really couldn't find the file.
654 //===----------------------------------------------------------------------===//
655 // Preprocessor Directive Handling.
656 //===----------------------------------------------------------------------===//
658 class Preprocessor::ResetMacroExpansionHelper {
660 ResetMacroExpansionHelper(Preprocessor *pp)
661 : PP(pp), save(pp->DisableMacroExpansion) {
662 if (pp->MacroExpansionInDirectivesOverride)
663 pp->DisableMacroExpansion = false;
665 ~ResetMacroExpansionHelper() {
666 PP->DisableMacroExpansion = save;
673 /// HandleDirective - This callback is invoked when the lexer sees a # token
674 /// at the start of a line. This consumes the directive, modifies the
675 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
676 /// read is the correct one.
677 void Preprocessor::HandleDirective(Token &Result) {
678 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
680 // We just parsed a # character at the start of a line, so we're in directive
681 // mode. Tell the lexer this so any newlines we see will be converted into an
682 // EOD token (which terminates the directive).
683 CurPPLexer->ParsingPreprocessorDirective = true;
684 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
686 bool ImmediatelyAfterTopLevelIfndef =
687 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
688 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
692 // We are about to read a token. For the multiple-include optimization FA to
693 // work, we have to remember if we had read any tokens *before* this
695 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
697 // Save the '#' token in case we need to return it later.
698 Token SavedHash = Result;
700 // Read the next token, the directive flavor. This isn't expanded due to
702 LexUnexpandedToken(Result);
704 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
709 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
710 // not support this for #include-like directives, since that can result in
711 // terrible diagnostics, and does not work in GCC.
713 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
714 switch (II->getPPKeywordID()) {
715 case tok::pp_include:
717 case tok::pp_include_next:
718 case tok::pp___include_macros:
719 Diag(Result, diag::err_embedded_include) << II->getName();
720 DiscardUntilEndOfDirective();
726 Diag(Result, diag::ext_embedded_directive);
729 // Temporarily enable macro expansion if set so
730 // and reset to previous state when returning from this function.
731 ResetMacroExpansionHelper helper(this);
733 switch (Result.getKind()) {
735 return; // null directive.
736 case tok::code_completion:
738 CodeComplete->CodeCompleteDirective(
739 CurPPLexer->getConditionalStackDepth() > 0);
740 setCodeCompletionReached();
742 case tok::numeric_constant: // # 7 GNU line marker directive.
743 if (getLangOpts().AsmPreprocessor)
744 break; // # 4 is not a preprocessor directive in .S files.
745 return HandleDigitDirective(Result);
747 IdentifierInfo *II = Result.getIdentifierInfo();
748 if (!II) break; // Not an identifier.
750 // Ask what the preprocessor keyword ID is.
751 switch (II->getPPKeywordID()) {
753 // C99 6.10.1 - Conditional Inclusion.
755 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
757 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
759 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
761 return HandleElifDirective(Result);
763 return HandleElseDirective(Result);
765 return HandleEndifDirective(Result);
767 // C99 6.10.2 - Source File Inclusion.
768 case tok::pp_include:
770 return HandleIncludeDirective(SavedHash.getLocation(), Result);
771 case tok::pp___include_macros:
773 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
775 // C99 6.10.3 - Macro Replacement.
777 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
779 return HandleUndefDirective(Result);
781 // C99 6.10.4 - Line Control.
783 return HandleLineDirective(Result);
785 // C99 6.10.5 - Error Directive.
787 return HandleUserDiagnosticDirective(Result, false);
789 // C99 6.10.6 - Pragma Directive.
791 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
795 return HandleImportDirective(SavedHash.getLocation(), Result);
796 case tok::pp_include_next:
797 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
799 case tok::pp_warning:
800 Diag(Result, diag::ext_pp_warning_directive);
801 return HandleUserDiagnosticDirective(Result, true);
803 return HandleIdentSCCSDirective(Result);
805 return HandleIdentSCCSDirective(Result);
807 //isExtension = true; // FIXME: implement #assert
809 case tok::pp_unassert:
810 //isExtension = true; // FIXME: implement #unassert
813 case tok::pp___public_macro:
814 if (getLangOpts().Modules)
815 return HandleMacroPublicDirective(Result);
818 case tok::pp___private_macro:
819 if (getLangOpts().Modules)
820 return HandleMacroPrivateDirective(Result);
826 // If this is a .S file, treat unknown # directives as non-preprocessor
827 // directives. This is important because # may be a comment or introduce
828 // various pseudo-ops. Just return the # token and push back the following
829 // token to be lexed next time.
830 if (getLangOpts().AsmPreprocessor) {
831 Token *Toks = new Token[2];
832 // Return the # and the token after it.
836 // If the second token is a hashhash token, then we need to translate it to
837 // unknown so the token lexer doesn't try to perform token pasting.
838 if (Result.is(tok::hashhash))
839 Toks[1].setKind(tok::unknown);
841 // Enter this token stream so that we re-lex the tokens. Make sure to
842 // enable macro expansion, in case the token after the # is an identifier
844 EnterTokenStream(Toks, 2, false, true);
848 // If we reached here, the preprocessing token is not valid!
849 Diag(Result, diag::err_pp_invalid_directive);
851 // Read the rest of the PP line.
852 DiscardUntilEndOfDirective();
854 // Okay, we're done parsing the directive.
857 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
858 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
859 static bool GetLineValue(Token &DigitTok, unsigned &Val,
860 unsigned DiagID, Preprocessor &PP,
861 bool IsGNULineDirective=false) {
862 if (DigitTok.isNot(tok::numeric_constant)) {
863 PP.Diag(DigitTok, DiagID);
865 if (DigitTok.isNot(tok::eod))
866 PP.DiscardUntilEndOfDirective();
870 SmallString<64> IntegerBuffer;
871 IntegerBuffer.resize(DigitTok.getLength());
872 const char *DigitTokBegin = &IntegerBuffer[0];
873 bool Invalid = false;
874 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
878 // Verify that we have a simple digit-sequence, and compute the value. This
879 // is always a simple digit string computed in decimal, so we do this manually
882 for (unsigned i = 0; i != ActualLength; ++i) {
883 // C++1y [lex.fcon]p1:
884 // Optional separating single quotes in a digit-sequence are ignored
885 if (DigitTokBegin[i] == '\'')
888 if (!isDigit(DigitTokBegin[i])) {
889 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
890 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
891 PP.DiscardUntilEndOfDirective();
895 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
896 if (NextVal < Val) { // overflow.
897 PP.Diag(DigitTok, DiagID);
898 PP.DiscardUntilEndOfDirective();
904 if (DigitTokBegin[0] == '0' && Val)
905 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
906 << IsGNULineDirective;
911 /// \brief Handle a \#line directive: C99 6.10.4.
913 /// The two acceptable forms are:
915 /// # line digit-sequence
916 /// # line digit-sequence "s-char-sequence"
918 void Preprocessor::HandleLineDirective(Token &Tok) {
919 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
924 // Validate the number and convert it to an unsigned.
926 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
930 Diag(DigitTok, diag::ext_pp_line_zero);
932 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
933 // number greater than 2147483647". C90 requires that the line # be <= 32767.
934 unsigned LineLimit = 32768U;
935 if (LangOpts.C99 || LangOpts.CPlusPlus11)
936 LineLimit = 2147483648U;
937 if (LineNo >= LineLimit)
938 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
939 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
940 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
946 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
947 // string followed by eod.
948 if (StrTok.is(tok::eod))
950 else if (StrTok.isNot(tok::string_literal)) {
951 Diag(StrTok, diag::err_pp_line_invalid_filename);
952 return DiscardUntilEndOfDirective();
953 } else if (StrTok.hasUDSuffix()) {
954 Diag(StrTok, diag::err_invalid_string_udl);
955 return DiscardUntilEndOfDirective();
957 // Parse and validate the string, converting it into a unique ID.
958 StringLiteralParser Literal(StrTok, *this);
959 assert(Literal.isAscii() && "Didn't allow wide strings in");
960 if (Literal.hadError)
961 return DiscardUntilEndOfDirective();
962 if (Literal.Pascal) {
963 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
964 return DiscardUntilEndOfDirective();
966 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
968 // Verify that there is nothing after the string, other than EOD. Because
969 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
970 CheckEndOfDirective("line", true);
973 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
976 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
977 PPCallbacks::RenameFile,
981 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
982 /// marker directive.
983 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
984 bool &IsSystemHeader, bool &IsExternCHeader,
989 if (FlagTok.is(tok::eod)) return false;
990 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
997 if (FlagTok.is(tok::eod)) return false;
998 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1000 } else if (FlagVal == 2) {
1003 SourceManager &SM = PP.getSourceManager();
1004 // If we are leaving the current presumed file, check to make sure the
1005 // presumed include stack isn't empty!
1007 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1008 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1009 if (PLoc.isInvalid())
1012 // If there is no include loc (main file) or if the include loc is in a
1013 // different physical file, then we aren't in a "1" line marker flag region.
1014 SourceLocation IncLoc = PLoc.getIncludeLoc();
1015 if (IncLoc.isInvalid() ||
1016 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1017 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1018 PP.DiscardUntilEndOfDirective();
1023 if (FlagTok.is(tok::eod)) return false;
1024 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1028 // We must have 3 if there are still flags.
1030 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1031 PP.DiscardUntilEndOfDirective();
1035 IsSystemHeader = true;
1038 if (FlagTok.is(tok::eod)) return false;
1039 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1042 // We must have 4 if there is yet another flag.
1044 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1045 PP.DiscardUntilEndOfDirective();
1049 IsExternCHeader = true;
1052 if (FlagTok.is(tok::eod)) return false;
1054 // There are no more valid flags here.
1055 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1056 PP.DiscardUntilEndOfDirective();
1060 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1061 /// one of the following forms:
1064 /// # 42 "file" ('1' | '2')?
1065 /// # 42 "file" ('1' | '2')? '3' '4'?
1067 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1068 // Validate the number and convert it to an unsigned. GNU does not have a
1069 // line # limit other than it fit in 32-bits.
1071 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1078 bool IsFileEntry = false, IsFileExit = false;
1079 bool IsSystemHeader = false, IsExternCHeader = false;
1080 int FilenameID = -1;
1082 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1083 // string followed by eod.
1084 if (StrTok.is(tok::eod))
1086 else if (StrTok.isNot(tok::string_literal)) {
1087 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1088 return DiscardUntilEndOfDirective();
1089 } else if (StrTok.hasUDSuffix()) {
1090 Diag(StrTok, diag::err_invalid_string_udl);
1091 return DiscardUntilEndOfDirective();
1093 // Parse and validate the string, converting it into a unique ID.
1094 StringLiteralParser Literal(StrTok, *this);
1095 assert(Literal.isAscii() && "Didn't allow wide strings in");
1096 if (Literal.hadError)
1097 return DiscardUntilEndOfDirective();
1098 if (Literal.Pascal) {
1099 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1100 return DiscardUntilEndOfDirective();
1102 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1104 // If a filename was present, read any flags that are present.
1105 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1106 IsSystemHeader, IsExternCHeader, *this))
1110 // Create a line note with this information.
1111 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1112 IsFileEntry, IsFileExit,
1113 IsSystemHeader, IsExternCHeader);
1115 // If the preprocessor has callbacks installed, notify them of the #line
1116 // change. This is used so that the line marker comes out in -E mode for
1119 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1121 Reason = PPCallbacks::EnterFile;
1122 else if (IsFileExit)
1123 Reason = PPCallbacks::ExitFile;
1124 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1125 if (IsExternCHeader)
1126 FileKind = SrcMgr::C_ExternCSystem;
1127 else if (IsSystemHeader)
1128 FileKind = SrcMgr::C_System;
1130 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1135 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1137 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1139 // PTH doesn't emit #warning or #error directives.
1141 return CurPTHLexer->DiscardToEndOfLine();
1143 // Read the rest of the line raw. We do this because we don't want macros
1144 // to be expanded and we don't require that the tokens be valid preprocessing
1145 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1146 // collapse multiple consequtive white space between tokens, but this isn't
1147 // specified by the standard.
1148 SmallString<128> Message;
1149 CurLexer->ReadToEndOfLine(&Message);
1151 // Find the first non-whitespace character, so that we can make the
1152 // diagnostic more succinct.
1153 StringRef Msg = Message.str().ltrim(" ");
1156 Diag(Tok, diag::pp_hash_warning) << Msg;
1158 Diag(Tok, diag::err_pp_hash_error) << Msg;
1161 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1163 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1164 // Yes, this directive is an extension.
1165 Diag(Tok, diag::ext_pp_ident_directive);
1167 // Read the string argument.
1171 // If the token kind isn't a string, it's a malformed directive.
1172 if (StrTok.isNot(tok::string_literal) &&
1173 StrTok.isNot(tok::wide_string_literal)) {
1174 Diag(StrTok, diag::err_pp_malformed_ident);
1175 if (StrTok.isNot(tok::eod))
1176 DiscardUntilEndOfDirective();
1180 if (StrTok.hasUDSuffix()) {
1181 Diag(StrTok, diag::err_invalid_string_udl);
1182 return DiscardUntilEndOfDirective();
1185 // Verify that there is nothing after the string, other than EOD.
1186 CheckEndOfDirective("ident");
1189 bool Invalid = false;
1190 std::string Str = getSpelling(StrTok, &Invalid);
1192 Callbacks->Ident(Tok.getLocation(), Str);
1196 /// \brief Handle a #public directive.
1197 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1199 ReadMacroName(MacroNameTok, 2);
1201 // Error reading macro name? If so, diagnostic already issued.
1202 if (MacroNameTok.is(tok::eod))
1205 // Check to see if this is the last token on the #__public_macro line.
1206 CheckEndOfDirective("__public_macro");
1208 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1209 // Okay, we finally have a valid identifier to undef.
1210 MacroDirective *MD = getMacroDirective(II);
1212 // If the macro is not defined, this is an error.
1214 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1218 // Note that this macro has now been exported.
1219 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1220 MacroNameTok.getLocation(), /*IsPublic=*/true));
1223 /// \brief Handle a #private directive.
1224 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1226 ReadMacroName(MacroNameTok, 2);
1228 // Error reading macro name? If so, diagnostic already issued.
1229 if (MacroNameTok.is(tok::eod))
1232 // Check to see if this is the last token on the #__private_macro line.
1233 CheckEndOfDirective("__private_macro");
1235 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1236 // Okay, we finally have a valid identifier to undef.
1237 MacroDirective *MD = getMacroDirective(II);
1239 // If the macro is not defined, this is an error.
1241 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1245 // Note that this macro has now been marked private.
1246 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1247 MacroNameTok.getLocation(), /*IsPublic=*/false));
1250 //===----------------------------------------------------------------------===//
1251 // Preprocessor Include Directive Handling.
1252 //===----------------------------------------------------------------------===//
1254 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1255 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1256 /// true if the input filename was in <>'s or false if it were in ""'s. The
1257 /// caller is expected to provide a buffer that is large enough to hold the
1258 /// spelling of the filename, but is also expected to handle the case when
1259 /// this method decides to use a different buffer.
1260 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1261 StringRef &Buffer) {
1262 // Get the text form of the filename.
1263 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1265 // Make sure the filename is <x> or "x".
1267 if (Buffer[0] == '<') {
1268 if (Buffer.back() != '>') {
1269 Diag(Loc, diag::err_pp_expects_filename);
1270 Buffer = StringRef();
1274 } else if (Buffer[0] == '"') {
1275 if (Buffer.back() != '"') {
1276 Diag(Loc, diag::err_pp_expects_filename);
1277 Buffer = StringRef();
1282 Diag(Loc, diag::err_pp_expects_filename);
1283 Buffer = StringRef();
1287 // Diagnose #include "" as invalid.
1288 if (Buffer.size() <= 2) {
1289 Diag(Loc, diag::err_pp_empty_filename);
1290 Buffer = StringRef();
1294 // Skip the brackets.
1295 Buffer = Buffer.substr(1, Buffer.size()-2);
1299 // \brief Handle cases where the \#include name is expanded from a macro
1300 // as multiple tokens, which need to be glued together.
1302 // This occurs for code like:
1304 // \#define FOO <a/b.h>
1307 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1309 // This code concatenates and consumes tokens up to the '>' token. It returns
1310 // false if the > was found, otherwise it returns true if it finds and consumes
1312 bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1313 SourceLocation &End) {
1317 while (CurTok.isNot(tok::eod)) {
1318 End = CurTok.getLocation();
1320 // FIXME: Provide code completion for #includes.
1321 if (CurTok.is(tok::code_completion)) {
1322 setCodeCompletionReached();
1327 // Append the spelling of this token to the buffer. If there was a space
1328 // before it, add it now.
1329 if (CurTok.hasLeadingSpace())
1330 FilenameBuffer.push_back(' ');
1332 // Get the spelling of the token, directly into FilenameBuffer if possible.
1333 unsigned PreAppendSize = FilenameBuffer.size();
1334 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1336 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1337 unsigned ActualLen = getSpelling(CurTok, BufPtr);
1339 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1340 if (BufPtr != &FilenameBuffer[PreAppendSize])
1341 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1343 // Resize FilenameBuffer to the correct size.
1344 if (CurTok.getLength() != ActualLen)
1345 FilenameBuffer.resize(PreAppendSize+ActualLen);
1347 // If we found the '>' marker, return success.
1348 if (CurTok.is(tok::greater))
1354 // If we hit the eod marker, emit an error and return true so that the caller
1355 // knows the EOD has been read.
1356 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1360 /// \brief Push a token onto the token stream containing an annotation.
1361 static void EnterAnnotationToken(Preprocessor &PP,
1362 SourceLocation Begin, SourceLocation End,
1363 tok::TokenKind Kind, void *AnnotationVal) {
1364 Token *Tok = new Token[1];
1365 Tok[0].startToken();
1366 Tok[0].setKind(Kind);
1367 Tok[0].setLocation(Begin);
1368 Tok[0].setAnnotationEndLoc(End);
1369 Tok[0].setAnnotationValue(AnnotationVal);
1370 PP.EnterTokenStream(Tok, 1, true, true);
1373 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1374 /// the file to be included from the lexer, then include it! This is a common
1375 /// routine with functionality shared between \#include, \#include_next and
1376 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1377 /// specifies the file to start searching from.
1378 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1380 const DirectoryLookup *LookupFrom,
1384 CurPPLexer->LexIncludeFilename(FilenameTok);
1386 // Reserve a buffer to get the spelling.
1387 SmallString<128> FilenameBuffer;
1390 SourceLocation CharEnd; // the end of this directive, in characters
1392 switch (FilenameTok.getKind()) {
1394 // If the token kind is EOD, the error has already been diagnosed.
1397 case tok::angle_string_literal:
1398 case tok::string_literal:
1399 Filename = getSpelling(FilenameTok, FilenameBuffer);
1400 End = FilenameTok.getLocation();
1401 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1405 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1406 // case, glue the tokens together into FilenameBuffer and interpret those.
1407 FilenameBuffer.push_back('<');
1408 if (ConcatenateIncludeName(FilenameBuffer, End))
1409 return; // Found <eod> but no ">"? Diagnostic already emitted.
1410 Filename = FilenameBuffer.str();
1411 CharEnd = End.getLocWithOffset(1);
1414 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1415 DiscardUntilEndOfDirective();
1419 CharSourceRange FilenameRange
1420 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1421 StringRef OriginalFilename = Filename;
1423 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1424 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1426 if (Filename.empty()) {
1427 DiscardUntilEndOfDirective();
1431 // Verify that there is nothing after the filename, other than EOD. Note that
1432 // we allow macros that expand to nothing after the filename, because this
1433 // falls into the category of "#include pp-tokens new-line" specified in
1435 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1437 // Check that we don't have infinite #include recursion.
1438 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1439 Diag(FilenameTok, diag::err_pp_include_too_deep);
1443 // Complain about attempts to #include files in an audit pragma.
1444 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1445 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1446 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1448 // Immediately leave the pragma.
1449 PragmaARCCFCodeAuditedLoc = SourceLocation();
1452 if (HeaderInfo.HasIncludeAliasMap()) {
1453 // Map the filename with the brackets still attached. If the name doesn't
1454 // map to anything, fall back on the filename we've already gotten the
1456 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1457 if (!NewName.empty())
1461 // Search include directories.
1462 const DirectoryLookup *CurDir;
1463 SmallString<1024> SearchPath;
1464 SmallString<1024> RelativePath;
1465 // We get the raw path only if we have 'Callbacks' to which we later pass
1467 ModuleMap::KnownHeader SuggestedModule;
1468 SourceLocation FilenameLoc = FilenameTok.getLocation();
1469 SmallString<128> NormalizedPath;
1470 if (LangOpts.MSVCCompat) {
1471 NormalizedPath = Filename.str();
1472 llvm::sys::fs::normalize_separators(NormalizedPath);
1474 const FileEntry *File = LookupFile(
1475 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1476 isAngled, LookupFrom, CurDir, Callbacks ? &SearchPath : nullptr,
1477 Callbacks ? &RelativePath : nullptr,
1478 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule : nullptr);
1482 // Give the clients a chance to recover.
1483 SmallString<128> RecoveryPath;
1484 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1485 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1486 // Add the recovery path to the list of search paths.
1487 DirectoryLookup DL(DE, SrcMgr::C_User, false);
1488 HeaderInfo.AddSearchPath(DL, isAngled);
1490 // Try the lookup again, skipping the cache.
1491 File = LookupFile(FilenameLoc,
1492 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1494 isAngled, LookupFrom, CurDir, nullptr, nullptr,
1495 HeaderInfo.getHeaderSearchOpts().ModuleMaps
1498 /*SkipCache*/ true);
1503 if (!SuggestedModule || !getLangOpts().Modules) {
1504 // Notify the callback object that we've seen an inclusion directive.
1505 Callbacks->InclusionDirective(HashLoc, IncludeTok,
1506 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1508 isAngled, FilenameRange, File, SearchPath,
1509 RelativePath, /*ImportedModule=*/nullptr);
1514 if (!SuppressIncludeNotFoundError) {
1515 // If the file could not be located and it was included via angle
1516 // brackets, we can attempt a lookup as though it were a quoted path to
1517 // provide the user with a possible fixit.
1520 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1521 false, LookupFrom, CurDir, Callbacks ? &SearchPath : nullptr,
1522 Callbacks ? &RelativePath : nullptr,
1523 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule
1526 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1527 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1529 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1532 // If the file is still not found, just go with the vanilla diagnostic
1534 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1540 // If we are supposed to import a module rather than including the header,
1542 if (SuggestedModule && getLangOpts().Modules) {
1543 // Compute the module access path corresponding to this module.
1544 // FIXME: Should we have a second loadModule() overload to avoid this
1545 // extra lookup step?
1546 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1547 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1548 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1549 FilenameTok.getLocation()));
1550 std::reverse(Path.begin(), Path.end());
1552 // Warn that we're replacing the include/import with a module import.
1553 SmallString<128> PathString;
1554 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1557 PathString += Path[I].first->getName();
1559 int IncludeKind = 0;
1561 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1562 case tok::pp_include:
1566 case tok::pp_import:
1570 case tok::pp_include_next:
1574 case tok::pp___include_macros:
1579 llvm_unreachable("unknown include directive kind");
1582 // Determine whether we are actually building the module that this
1583 // include directive maps to.
1584 bool BuildingImportedModule
1585 = Path[0].first->getName() == getLangOpts().CurrentModule;
1587 if (!BuildingImportedModule && getLangOpts().ObjC2) {
1588 // If we're not building the imported module, warn that we're going
1589 // to automatically turn this inclusion directive into a module import.
1590 // We only do this in Objective-C, where we have a module-import syntax.
1591 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd),
1592 /*IsTokenRange=*/false);
1593 Diag(HashLoc, diag::warn_auto_module_import)
1594 << IncludeKind << PathString
1595 << FixItHint::CreateReplacement(ReplaceRange,
1596 "@import " + PathString.str().str() + ";");
1599 // Load the module. Only make macros visible. We'll make the declarations
1600 // visible when the parser gets here.
1601 Module::NameVisibilityKind Visibility = Module::MacrosVisible;
1602 ModuleLoadResult Imported
1603 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1604 /*IsIncludeDirective=*/true);
1605 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1606 "the imported module is different than the suggested one");
1608 if (!Imported && hadModuleLoaderFatalFailure()) {
1609 // With a fatal failure in the module loader, we abort parsing.
1610 Token &Result = IncludeTok;
1612 Result.startToken();
1613 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1614 CurLexer->cutOffLexing();
1616 assert(CurPTHLexer && "#include but no current lexer set!");
1617 CurPTHLexer->getEOF(Result);
1622 // If this header isn't part of the module we're building, we're done.
1623 if (!BuildingImportedModule && Imported) {
1625 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1626 FilenameRange, File,
1627 SearchPath, RelativePath, Imported);
1630 if (IncludeKind != 3) {
1631 // Let the parser know that we hit a module import, and it should
1632 // make the module visible.
1633 // FIXME: Produce this as the current token directly, rather than
1634 // allocating a new token for it.
1635 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include,
1641 // If we failed to find a submodule that we expected to find, we can
1642 // continue. Otherwise, there's an error in the included file, so we
1643 // don't want to include it.
1644 if (!BuildingImportedModule && !Imported.isMissingExpected()) {
1649 if (Callbacks && SuggestedModule) {
1650 // We didn't notify the callback object that we've seen an inclusion
1651 // directive before. Now that we are parsing the include normally and not
1652 // turning it to a module import, notify the callback object.
1653 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1654 FilenameRange, File,
1655 SearchPath, RelativePath,
1656 /*ImportedModule=*/nullptr);
1659 // The #included file will be considered to be a system header if either it is
1660 // in a system include directory, or if the #includer is a system include
1662 SrcMgr::CharacteristicKind FileCharacter =
1663 std::max(HeaderInfo.getFileDirFlavor(File),
1664 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1666 // Ask HeaderInfo if we should enter this #include file. If not, #including
1667 // this file will have no effect.
1668 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1670 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1674 // Look up the file, create a File ID for it.
1675 SourceLocation IncludePos = End;
1676 // If the filename string was the result of macro expansions, set the include
1677 // position on the file where it will be included and after the expansions.
1678 if (IncludePos.isMacroID())
1679 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1680 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
1681 assert(!FID.isInvalid() && "Expected valid file ID");
1683 // Determine if we're switching to building a new submodule, and which one.
1684 ModuleMap::KnownHeader BuildingModule;
1685 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty()) {
1686 Module *RequestingModule = getModuleForLocation(FilenameLoc);
1688 HeaderInfo.getModuleMap().findModuleForHeader(File, RequestingModule);
1691 // If all is good, enter the new file!
1692 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1695 // If we're walking into another part of the same module, let the parser
1696 // know that any future declarations are within that other submodule.
1697 if (BuildingModule) {
1698 assert(!CurSubmodule && "should not have marked this as a module yet");
1699 CurSubmodule = BuildingModule.getModule();
1701 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin,
1706 /// HandleIncludeNextDirective - Implements \#include_next.
1708 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1709 Token &IncludeNextTok) {
1710 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1712 // #include_next is like #include, except that we start searching after
1713 // the current found directory. If we can't do this, issue a
1715 const DirectoryLookup *Lookup = CurDirLookup;
1716 if (isInPrimaryFile()) {
1718 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1719 } else if (!Lookup) {
1720 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1722 // Start looking up in the next directory.
1726 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
1729 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
1730 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1731 // The Microsoft #import directive takes a type library and generates header
1732 // files from it, and includes those. This is beyond the scope of what clang
1733 // does, so we ignore it and error out. However, #import can optionally have
1734 // trailing attributes that span multiple lines. We're going to eat those
1735 // so we can continue processing from there.
1736 Diag(Tok, diag::err_pp_import_directive_ms );
1738 // Read tokens until we get to the end of the directive. Note that the
1739 // directive can be split over multiple lines using the backslash character.
1740 DiscardUntilEndOfDirective();
1743 /// HandleImportDirective - Implements \#import.
1745 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1747 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
1748 if (LangOpts.MSVCCompat)
1749 return HandleMicrosoftImportDirective(ImportTok);
1750 Diag(ImportTok, diag::ext_pp_import_directive);
1752 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, true);
1755 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1756 /// pseudo directive in the predefines buffer. This handles it by sucking all
1757 /// tokens through the preprocessor and discarding them (only keeping the side
1758 /// effects on the preprocessor).
1759 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1760 Token &IncludeMacrosTok) {
1761 // This directive should only occur in the predefines buffer. If not, emit an
1762 // error and reject it.
1763 SourceLocation Loc = IncludeMacrosTok.getLocation();
1764 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1765 Diag(IncludeMacrosTok.getLocation(),
1766 diag::pp_include_macros_out_of_predefines);
1767 DiscardUntilEndOfDirective();
1771 // Treat this as a normal #include for checking purposes. If this is
1772 // successful, it will push a new lexer onto the include stack.
1773 HandleIncludeDirective(HashLoc, IncludeMacrosTok, nullptr, false);
1778 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1779 } while (TmpTok.isNot(tok::hashhash));
1782 //===----------------------------------------------------------------------===//
1783 // Preprocessor Macro Directive Handling.
1784 //===----------------------------------------------------------------------===//
1786 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1787 /// definition has just been read. Lex the rest of the arguments and the
1788 /// closing ), updating MI with what we learn. Return true if an error occurs
1789 /// parsing the arg list.
1790 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
1791 SmallVector<IdentifierInfo*, 32> Arguments;
1794 LexUnexpandedToken(Tok);
1795 switch (Tok.getKind()) {
1797 // Found the end of the argument list.
1798 if (Arguments.empty()) // #define FOO()
1800 // Otherwise we have #define FOO(A,)
1801 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1803 case tok::ellipsis: // #define X(... -> C99 varargs
1805 Diag(Tok, LangOpts.CPlusPlus11 ?
1806 diag::warn_cxx98_compat_variadic_macro :
1807 diag::ext_variadic_macro);
1809 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1810 if (LangOpts.OpenCL) {
1811 Diag(Tok, diag::err_pp_opencl_variadic_macros);
1815 // Lex the token after the identifier.
1816 LexUnexpandedToken(Tok);
1817 if (Tok.isNot(tok::r_paren)) {
1818 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1821 // Add the __VA_ARGS__ identifier as an argument.
1822 Arguments.push_back(Ident__VA_ARGS__);
1823 MI->setIsC99Varargs();
1824 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1826 case tok::eod: // #define X(
1827 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1830 // Handle keywords and identifiers here to accept things like
1831 // #define Foo(for) for.
1832 IdentifierInfo *II = Tok.getIdentifierInfo();
1835 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1839 // If this is already used as an argument, it is used multiple times (e.g.
1841 if (std::find(Arguments.begin(), Arguments.end(), II) !=
1842 Arguments.end()) { // C99 6.10.3p6
1843 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1847 // Add the argument to the macro info.
1848 Arguments.push_back(II);
1850 // Lex the token after the identifier.
1851 LexUnexpandedToken(Tok);
1853 switch (Tok.getKind()) {
1854 default: // #define X(A B
1855 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1857 case tok::r_paren: // #define X(A)
1858 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1860 case tok::comma: // #define X(A,
1862 case tok::ellipsis: // #define X(A... -> GCC extension
1863 // Diagnose extension.
1864 Diag(Tok, diag::ext_named_variadic_macro);
1866 // Lex the token after the identifier.
1867 LexUnexpandedToken(Tok);
1868 if (Tok.isNot(tok::r_paren)) {
1869 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1873 MI->setIsGNUVarargs();
1874 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1881 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
1882 /// line then lets the caller lex the next real token.
1883 void Preprocessor::HandleDefineDirective(Token &DefineTok,
1884 bool ImmediatelyAfterHeaderGuard) {
1888 ReadMacroName(MacroNameTok, 1);
1890 // Error reading macro name? If so, diagnostic already issued.
1891 if (MacroNameTok.is(tok::eod))
1894 Token LastTok = MacroNameTok;
1896 // If we are supposed to keep comments in #defines, reenable comment saving
1898 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
1900 // Create the new macro.
1901 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
1904 LexUnexpandedToken(Tok);
1906 // If this is a function-like macro definition, parse the argument list,
1907 // marking each of the identifiers as being used as macro arguments. Also,
1908 // check other constraints on the first token of the macro body.
1909 if (Tok.is(tok::eod)) {
1910 if (ImmediatelyAfterHeaderGuard) {
1911 // Save this macro information since it may part of a header guard.
1912 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
1913 MacroNameTok.getLocation());
1915 // If there is no body to this macro, we have no special handling here.
1916 } else if (Tok.hasLeadingSpace()) {
1917 // This is a normal token with leading space. Clear the leading space
1918 // marker on the first token to get proper expansion.
1919 Tok.clearFlag(Token::LeadingSpace);
1920 } else if (Tok.is(tok::l_paren)) {
1921 // This is a function-like macro definition. Read the argument list.
1922 MI->setIsFunctionLike();
1923 if (ReadMacroDefinitionArgList(MI, LastTok)) {
1925 ReleaseMacroInfo(MI);
1926 // Throw away the rest of the line.
1927 if (CurPPLexer->ParsingPreprocessorDirective)
1928 DiscardUntilEndOfDirective();
1932 // If this is a definition of a variadic C99 function-like macro, not using
1933 // the GNU named varargs extension, enabled __VA_ARGS__.
1935 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1936 // This gets unpoisoned where it is allowed.
1937 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1938 if (MI->isC99Varargs())
1939 Ident__VA_ARGS__->setIsPoisoned(false);
1941 // Read the first token after the arg list for down below.
1942 LexUnexpandedToken(Tok);
1943 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
1944 // C99 requires whitespace between the macro definition and the body. Emit
1945 // a diagnostic for something like "#define X+".
1946 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
1948 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1949 // first character of a replacement list is not a character required by
1950 // subclause 5.2.1, then there shall be white-space separation between the
1951 // identifier and the replacement list.". 5.2.1 lists this set:
1952 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1953 // is irrelevant here.
1954 bool isInvalid = false;
1955 if (Tok.is(tok::at)) // @ is not in the list above.
1957 else if (Tok.is(tok::unknown)) {
1958 // If we have an unknown token, it is something strange like "`". Since
1959 // all of valid characters would have lexed into a single character
1960 // token of some sort, we know this is not a valid case.
1964 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1966 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
1969 if (!Tok.is(tok::eod))
1972 // Read the rest of the macro body.
1973 if (MI->isObjectLike()) {
1974 // Object-like macros are very simple, just read their body.
1975 while (Tok.isNot(tok::eod)) {
1977 MI->AddTokenToBody(Tok);
1978 // Get the next token of the macro.
1979 LexUnexpandedToken(Tok);
1983 // Otherwise, read the body of a function-like macro. While we are at it,
1984 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1985 // parameters in function-like macro expansions.
1986 while (Tok.isNot(tok::eod)) {
1989 if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
1990 MI->AddTokenToBody(Tok);
1992 // Get the next token of the macro.
1993 LexUnexpandedToken(Tok);
1997 // If we're in -traditional mode, then we should ignore stringification
1998 // and token pasting. Mark the tokens as unknown so as not to confuse
2000 if (getLangOpts().TraditionalCPP) {
2001 Tok.setKind(tok::unknown);
2002 MI->AddTokenToBody(Tok);
2004 // Get the next token of the macro.
2005 LexUnexpandedToken(Tok);
2009 if (Tok.is(tok::hashhash)) {
2011 // If we see token pasting, check if it looks like the gcc comma
2012 // pasting extension. We'll use this information to suppress
2013 // diagnostics later on.
2015 // Get the next token of the macro.
2016 LexUnexpandedToken(Tok);
2018 if (Tok.is(tok::eod)) {
2019 MI->AddTokenToBody(LastTok);
2023 unsigned NumTokens = MI->getNumTokens();
2024 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2025 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2026 MI->setHasCommaPasting();
2028 // Things look ok, add the '##' token to the macro.
2029 MI->AddTokenToBody(LastTok);
2033 // Get the next token of the macro.
2034 LexUnexpandedToken(Tok);
2036 // Check for a valid macro arg identifier.
2037 if (Tok.getIdentifierInfo() == nullptr ||
2038 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2040 // If this is assembler-with-cpp mode, we accept random gibberish after
2041 // the '#' because '#' is often a comment character. However, change
2042 // the kind of the token to tok::unknown so that the preprocessor isn't
2044 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2045 LastTok.setKind(tok::unknown);
2046 MI->AddTokenToBody(LastTok);
2049 Diag(Tok, diag::err_pp_stringize_not_parameter);
2050 ReleaseMacroInfo(MI);
2052 // Disable __VA_ARGS__ again.
2053 Ident__VA_ARGS__->setIsPoisoned(true);
2058 // Things look ok, add the '#' and param name tokens to the macro.
2059 MI->AddTokenToBody(LastTok);
2060 MI->AddTokenToBody(Tok);
2063 // Get the next token of the macro.
2064 LexUnexpandedToken(Tok);
2069 // Disable __VA_ARGS__ again.
2070 Ident__VA_ARGS__->setIsPoisoned(true);
2072 // Check that there is no paste (##) operator at the beginning or end of the
2073 // replacement list.
2074 unsigned NumTokens = MI->getNumTokens();
2075 if (NumTokens != 0) {
2076 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2077 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2078 ReleaseMacroInfo(MI);
2081 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2082 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2083 ReleaseMacroInfo(MI);
2088 MI->setDefinitionEndLoc(LastTok.getLocation());
2090 // Finally, if this identifier already had a macro defined for it, verify that
2091 // the macro bodies are identical, and issue diagnostics if they are not.
2092 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2093 // It is very common for system headers to have tons of macro redefinitions
2094 // and for warnings to be disabled in system headers. If this is the case,
2095 // then don't bother calling MacroInfo::isIdenticalTo.
2096 if (!getDiagnostics().getSuppressSystemWarnings() ||
2097 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2098 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2099 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2101 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2102 // C++ [cpp.predefined]p4, but allow it as an extension.
2103 if (OtherMI->isBuiltinMacro())
2104 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2105 // Macros must be identical. This means all tokens and whitespace
2106 // separation must be the same. C99 6.10.3p2.
2107 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2108 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2109 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2110 << MacroNameTok.getIdentifierInfo();
2111 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2114 if (OtherMI->isWarnIfUnused())
2115 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2118 DefMacroDirective *MD =
2119 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2121 assert(!MI->isUsed());
2122 // If we need warning for not using the macro, add its location in the
2123 // warn-because-unused-macro set. If it gets used it will be removed from set.
2124 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
2125 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2126 MI->setIsWarnIfUnused(true);
2127 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2130 // If the callbacks want to know, tell them about the macro definition.
2132 Callbacks->MacroDefined(MacroNameTok, MD);
2135 /// HandleUndefDirective - Implements \#undef.
2137 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2141 ReadMacroName(MacroNameTok, 2);
2143 // Error reading macro name? If so, diagnostic already issued.
2144 if (MacroNameTok.is(tok::eod))
2147 // Check to see if this is the last token on the #undef line.
2148 CheckEndOfDirective("undef");
2150 // Okay, we finally have a valid identifier to undef.
2151 MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo());
2152 const MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
2154 // If the callbacks want to know, tell them about the macro #undef.
2155 // Note: no matter if the macro was defined or not.
2157 Callbacks->MacroUndefined(MacroNameTok, MD);
2159 // If the macro is not defined, this is a noop undef, just return.
2163 if (!MI->isUsed() && MI->isWarnIfUnused())
2164 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2166 if (MI->isWarnIfUnused())
2167 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2169 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2170 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
2174 //===----------------------------------------------------------------------===//
2175 // Preprocessor Conditional Directive Handling.
2176 //===----------------------------------------------------------------------===//
2178 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2179 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2180 /// true if any tokens have been returned or pp-directives activated before this
2181 /// \#ifndef has been lexed.
2183 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2184 bool ReadAnyTokensBeforeDirective) {
2186 Token DirectiveTok = Result;
2189 ReadMacroName(MacroNameTok);
2191 // Error reading macro name? If so, diagnostic already issued.
2192 if (MacroNameTok.is(tok::eod)) {
2193 // Skip code until we get to #endif. This helps with recovery by not
2194 // emitting an error when the #endif is reached.
2195 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2196 /*Foundnonskip*/false, /*FoundElse*/false);
2200 // Check to see if this is the last token on the #if[n]def line.
2201 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2203 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2204 MacroDirective *MD = getMacroDirective(MII);
2205 MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
2207 if (CurPPLexer->getConditionalStackDepth() == 0) {
2208 // If the start of a top-level #ifdef and if the macro is not defined,
2209 // inform MIOpt that this might be the start of a proper include guard.
2210 // Otherwise it is some other form of unknown conditional which we can't
2212 if (!ReadAnyTokensBeforeDirective && !MI) {
2213 assert(isIfndef && "#ifdef shouldn't reach here");
2214 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2216 CurPPLexer->MIOpt.EnterTopLevelConditional();
2219 // If there is a macro, process it.
2220 if (MI) // Mark it used.
2221 markMacroAsUsed(MI);
2225 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2227 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2230 // Should we include the stuff contained by this directive?
2231 if (!MI == isIfndef) {
2232 // Yes, remember that we are inside a conditional, then lex the next token.
2233 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2234 /*wasskip*/false, /*foundnonskip*/true,
2235 /*foundelse*/false);
2237 // No, skip the contents of this block.
2238 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2239 /*Foundnonskip*/false,
2240 /*FoundElse*/false);
2244 /// HandleIfDirective - Implements the \#if directive.
2246 void Preprocessor::HandleIfDirective(Token &IfToken,
2247 bool ReadAnyTokensBeforeDirective) {
2250 // Parse and evaluate the conditional expression.
2251 IdentifierInfo *IfNDefMacro = nullptr;
2252 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2253 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2254 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2256 // If this condition is equivalent to #ifndef X, and if this is the first
2257 // directive seen, handle it for the multiple-include optimization.
2258 if (CurPPLexer->getConditionalStackDepth() == 0) {
2259 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2260 // FIXME: Pass in the location of the macro name, not the 'if' token.
2261 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2263 CurPPLexer->MIOpt.EnterTopLevelConditional();
2267 Callbacks->If(IfToken.getLocation(),
2268 SourceRange(ConditionalBegin, ConditionalEnd),
2269 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2271 // Should we include the stuff contained by this directive?
2272 if (ConditionalTrue) {
2273 // Yes, remember that we are inside a conditional, then lex the next token.
2274 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2275 /*foundnonskip*/true, /*foundelse*/false);
2277 // No, skip the contents of this block.
2278 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2279 /*FoundElse*/false);
2283 /// HandleEndifDirective - Implements the \#endif directive.
2285 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2288 // Check that this is the whole directive.
2289 CheckEndOfDirective("endif");
2291 PPConditionalInfo CondInfo;
2292 if (CurPPLexer->popConditionalLevel(CondInfo)) {
2293 // No conditionals on the stack: this is an #endif without an #if.
2294 Diag(EndifToken, diag::err_pp_endif_without_if);
2298 // If this the end of a top-level #endif, inform MIOpt.
2299 if (CurPPLexer->getConditionalStackDepth() == 0)
2300 CurPPLexer->MIOpt.ExitTopLevelConditional();
2302 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2303 "This code should only be reachable in the non-skipping case!");
2306 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2309 /// HandleElseDirective - Implements the \#else directive.
2311 void Preprocessor::HandleElseDirective(Token &Result) {
2314 // #else directive in a non-skipping conditional... start skipping.
2315 CheckEndOfDirective("else");
2317 PPConditionalInfo CI;
2318 if (CurPPLexer->popConditionalLevel(CI)) {
2319 Diag(Result, diag::pp_err_else_without_if);
2323 // If this is a top-level #else, inform the MIOpt.
2324 if (CurPPLexer->getConditionalStackDepth() == 0)
2325 CurPPLexer->MIOpt.EnterTopLevelConditional();
2327 // If this is a #else with a #else before it, report the error.
2328 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2331 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2333 // Finally, skip the rest of the contents of this block.
2334 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2335 /*FoundElse*/true, Result.getLocation());
2338 /// HandleElifDirective - Implements the \#elif directive.
2340 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2343 // #elif directive in a non-skipping conditional... start skipping.
2344 // We don't care what the condition is, because we will always skip it (since
2345 // the block immediately before it was included).
2346 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2347 DiscardUntilEndOfDirective();
2348 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2350 PPConditionalInfo CI;
2351 if (CurPPLexer->popConditionalLevel(CI)) {
2352 Diag(ElifToken, diag::pp_err_elif_without_if);
2356 // If this is a top-level #elif, inform the MIOpt.
2357 if (CurPPLexer->getConditionalStackDepth() == 0)
2358 CurPPLexer->MIOpt.EnterTopLevelConditional();
2360 // If this is a #elif with a #else before it, report the error.
2361 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2364 Callbacks->Elif(ElifToken.getLocation(),
2365 SourceRange(ConditionalBegin, ConditionalEnd),
2366 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
2368 // Finally, skip the rest of the contents of this block.
2369 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2370 /*FoundElse*/CI.FoundElse,
2371 ElifToken.getLocation());