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 //===----------------------------------------------------------------------===//
10 // This file implements # directive processing for the Preprocessor.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/Preprocessor.h"
15 #include "clang/Lex/LiteralSupport.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Lex/MacroInfo.h"
18 #include "clang/Lex/LexDiagnostic.h"
19 #include "clang/Lex/CodeCompletionHandler.h"
20 #include "clang/Lex/Pragma.h"
21 #include "clang/Basic/FileManager.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "llvm/ADT/APInt.h"
24 using namespace clang
;
26 //===----------------------------------------------------------------------===//
27 // Utility Methods for Preprocessor Directive Handling.
28 //===----------------------------------------------------------------------===//
30 MacroInfo
*Preprocessor::AllocateMacroInfo() {
31 MacroInfoChain
*MIChain
;
35 MICache
= MICache
->Next
;
38 MIChain
= BP
.Allocate
<MacroInfoChain
>();
41 MIChain
->Next
= MIChainHead
;
44 MIChainHead
->Prev
= MIChain
;
45 MIChainHead
= MIChain
;
47 return &(MIChain
->MI
);
50 MacroInfo
*Preprocessor::AllocateMacroInfo(SourceLocation L
) {
51 MacroInfo
*MI
= AllocateMacroInfo();
52 new (MI
) MacroInfo(L
);
56 MacroInfo
*Preprocessor::CloneMacroInfo(const MacroInfo
&MacroToClone
) {
57 MacroInfo
*MI
= AllocateMacroInfo();
58 new (MI
) MacroInfo(MacroToClone
, BP
);
62 /// ReleaseMacroInfo - Release the specified MacroInfo. This memory will
63 /// be reused for allocating new MacroInfo objects.
64 void Preprocessor::ReleaseMacroInfo(MacroInfo
*MI
) {
65 MacroInfoChain
*MIChain
= (MacroInfoChain
*) MI
;
66 if (MacroInfoChain
*Prev
= MIChain
->Prev
) {
67 MacroInfoChain
*Next
= MIChain
->Next
;
73 assert(MIChainHead
== MIChain
);
74 MIChainHead
= MIChain
->Next
;
75 MIChainHead
->Prev
= 0;
77 MIChain
->Next
= MICache
;
83 /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
84 /// current line until the tok::eom token is found.
85 void Preprocessor::DiscardUntilEndOfDirective() {
88 LexUnexpandedToken(Tmp
);
89 } while (Tmp
.isNot(tok::eom
));
92 /// ReadMacroName - Lex and validate a macro name, which occurs after a
93 /// #define or #undef. This sets the token kind to eom and discards the rest
94 /// of the macro line if the macro name is invalid. isDefineUndef is 1 if
95 /// this is due to a a #define, 2 if #undef directive, 0 if it is something
96 /// else (e.g. #ifdef).
97 void Preprocessor::ReadMacroName(Token
&MacroNameTok
, char isDefineUndef
) {
98 // Read the token, don't allow macro expansion on it.
99 LexUnexpandedToken(MacroNameTok
);
101 if (MacroNameTok
.is(tok::code_completion
)) {
103 CodeComplete
->CodeCompleteMacroName(isDefineUndef
== 1);
104 LexUnexpandedToken(MacroNameTok
);
108 // Missing macro name?
109 if (MacroNameTok
.is(tok::eom
)) {
110 Diag(MacroNameTok
, diag::err_pp_missing_macro_name
);
114 IdentifierInfo
*II
= MacroNameTok
.getIdentifierInfo();
116 bool Invalid
= false;
117 std::string Spelling
= getSpelling(MacroNameTok
, &Invalid
);
121 const IdentifierInfo
&Info
= Identifiers
.get(Spelling
);
122 if (Info
.isCPlusPlusOperatorKeyword())
123 // C++ 2.5p2: Alternative tokens behave the same as its primary token
124 // except for their spellings.
125 Diag(MacroNameTok
, diag::err_pp_operator_used_as_macro_name
) << Spelling
;
127 Diag(MacroNameTok
, diag::err_pp_macro_not_identifier
);
128 // Fall through on error.
129 } else if (isDefineUndef
&& II
->getPPKeywordID() == tok::pp_defined
) {
130 // Error if defining "defined": C99 6.10.8.4.
131 Diag(MacroNameTok
, diag::err_defined_macro_name
);
132 } else if (isDefineUndef
&& II
->hasMacroDefinition() &&
133 getMacroInfo(II
)->isBuiltinMacro()) {
134 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
135 if (isDefineUndef
== 1)
136 Diag(MacroNameTok
, diag::pp_redef_builtin_macro
);
138 Diag(MacroNameTok
, diag::pp_undef_builtin_macro
);
140 // Okay, we got a good identifier node. Return it.
144 // Invalid macro name, read and discard the rest of the line. Then set the
145 // token kind to tok::eom.
146 MacroNameTok
.setKind(tok::eom
);
147 return DiscardUntilEndOfDirective();
150 /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
151 /// not, emit a diagnostic and consume up until the eom. If EnableMacros is
152 /// true, then we consider macros that expand to zero tokens as being ok.
153 void Preprocessor::CheckEndOfDirective(const char *DirType
, bool EnableMacros
) {
155 // Lex unexpanded tokens for most directives: macros might expand to zero
156 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
157 // #line) allow empty macros.
161 LexUnexpandedToken(Tmp
);
163 // There should be no tokens after the directive, but we allow them as an
165 while (Tmp
.is(tok::comment
)) // Skip comments in -C mode.
166 LexUnexpandedToken(Tmp
);
168 if (Tmp
.isNot(tok::eom
)) {
169 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
170 // because it is more trouble than it is worth to insert /**/ and check that
171 // there is no /**/ in the range also.
173 if (Features
.GNUMode
|| Features
.C99
|| Features
.CPlusPlus
)
174 Hint
= FixItHint::CreateInsertion(Tmp
.getLocation(),"//");
175 Diag(Tmp
, diag::ext_pp_extra_tokens_at_eol
) << DirType
<< Hint
;
176 DiscardUntilEndOfDirective();
182 /// SkipExcludedConditionalBlock - We just read a #if or related directive and
183 /// decided that the subsequent tokens are in the #if'd out portion of the
184 /// file. Lex the rest of the file, until we see an #endif. If
185 /// FoundNonSkipPortion is true, then we have already emitted code for part of
186 /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
187 /// is true, then #else directives are ok, if not, then we have already seen one
188 /// so a #else directive is a duplicate. When this returns, the caller can lex
189 /// the first valid token.
190 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc
,
191 bool FoundNonSkipPortion
,
194 assert(CurTokenLexer
== 0 && CurPPLexer
&& "Lexing a macro, not a file?");
196 CurPPLexer
->pushConditionalLevel(IfTokenLoc
, /*isSkipping*/false,
197 FoundNonSkipPortion
, FoundElse
);
200 PTHSkipExcludedConditionalBlock();
204 // Enter raw mode to disable identifier lookup (and thus macro expansion),
205 // disabling warnings, etc.
206 CurPPLexer
->LexingRawMode
= true;
211 if (Tok
.is(tok::code_completion
)) {
213 CodeComplete
->CodeCompleteInConditionalExclusion();
217 // If this is the end of the buffer, we have an error.
218 if (Tok
.is(tok::eof
)) {
219 // Emit errors for each unterminated conditional on the stack, including
221 while (!CurPPLexer
->ConditionalStack
.empty()) {
222 if (!isCodeCompletionFile(Tok
.getLocation()))
223 Diag(CurPPLexer
->ConditionalStack
.back().IfLoc
,
224 diag::err_pp_unterminated_conditional
);
225 CurPPLexer
->ConditionalStack
.pop_back();
228 // Just return and let the caller lex after this #include.
232 // If this token is not a preprocessor directive, just skip it.
233 if (Tok
.isNot(tok::hash
) || !Tok
.isAtStartOfLine())
236 // We just parsed a # character at the start of a line, so we're in
237 // directive mode. Tell the lexer this so any newlines we see will be
238 // converted into an EOM token (this terminates the macro).
239 CurPPLexer
->ParsingPreprocessorDirective
= true;
240 if (CurLexer
) CurLexer
->SetCommentRetentionState(false);
243 // Read the next token, the directive flavor.
244 LexUnexpandedToken(Tok
);
246 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
247 // something bogus), skip it.
248 if (Tok
.isNot(tok::raw_identifier
)) {
249 CurPPLexer
->ParsingPreprocessorDirective
= false;
250 // Restore comment saving mode.
251 if (CurLexer
) CurLexer
->SetCommentRetentionState(KeepComments
);
255 // If the first letter isn't i or e, it isn't intesting to us. We know that
256 // this is safe in the face of spelling differences, because there is no way
257 // to spell an i/e in a strange way that is another letter. Skipping this
258 // allows us to avoid looking up the identifier info for #define/#undef and
259 // other common directives.
260 const char *RawCharData
= Tok
.getRawIdentifierData();
262 char FirstChar
= RawCharData
[0];
263 if (FirstChar
>= 'a' && FirstChar
<= 'z' &&
264 FirstChar
!= 'i' && FirstChar
!= 'e') {
265 CurPPLexer
->ParsingPreprocessorDirective
= false;
266 // Restore comment saving mode.
267 if (CurLexer
) CurLexer
->SetCommentRetentionState(KeepComments
);
271 // Get the identifier name without trigraphs or embedded newlines. Note
272 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
274 char DirectiveBuf
[20];
275 llvm::StringRef Directive
;
276 if (!Tok
.needsCleaning() && Tok
.getLength() < 20) {
277 Directive
= llvm::StringRef(RawCharData
, Tok
.getLength());
279 std::string DirectiveStr
= getSpelling(Tok
);
280 unsigned IdLen
= DirectiveStr
.size();
282 CurPPLexer
->ParsingPreprocessorDirective
= false;
283 // Restore comment saving mode.
284 if (CurLexer
) CurLexer
->SetCommentRetentionState(KeepComments
);
287 memcpy(DirectiveBuf
, &DirectiveStr
[0], IdLen
);
288 Directive
= llvm::StringRef(DirectiveBuf
, IdLen
);
291 if (Directive
.startswith("if")) {
292 llvm::StringRef Sub
= Directive
.substr(2);
293 if (Sub
.empty() || // "if"
294 Sub
== "def" || // "ifdef"
295 Sub
== "ndef") { // "ifndef"
296 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
297 // bother parsing the condition.
298 DiscardUntilEndOfDirective();
299 CurPPLexer
->pushConditionalLevel(Tok
.getLocation(), /*wasskipping*/true,
300 /*foundnonskip*/false,
306 } else if (Directive
[0] == 'e') {
307 llvm::StringRef Sub
= Directive
.substr(1);
308 if (Sub
== "ndif") { // "endif"
309 CheckEndOfDirective("endif");
310 PPConditionalInfo CondInfo
;
311 CondInfo
.WasSkipping
= true; // Silence bogus warning.
312 bool InCond
= CurPPLexer
->popConditionalLevel(CondInfo
);
313 (void)InCond
; // Silence warning in no-asserts mode.
314 assert(!InCond
&& "Can't be skipping if not in a conditional!");
316 // If we popped the outermost skipping block, we're done skipping!
317 if (!CondInfo
.WasSkipping
)
319 } else if (Sub
== "lse") { // "else".
320 // #else directive in a skipping conditional. If not in some other
321 // skipping conditional, and if #else hasn't already been seen, enter it
322 // as a non-skipping conditional.
323 DiscardUntilEndOfDirective(); // C99 6.10p4.
324 PPConditionalInfo
&CondInfo
= CurPPLexer
->peekConditionalLevel();
326 // If this is a #else with a #else before it, report the error.
327 if (CondInfo
.FoundElse
) Diag(Tok
, diag::pp_err_else_after_else
);
329 // Note that we've seen a #else in this conditional.
330 CondInfo
.FoundElse
= true;
335 // If the conditional is at the top level, and the #if block wasn't
336 // entered, enter the #else block now.
337 if (!CondInfo
.WasSkipping
&& !CondInfo
.FoundNonSkip
) {
338 CondInfo
.FoundNonSkip
= true;
341 } else if (Sub
== "lif") { // "elif".
342 PPConditionalInfo
&CondInfo
= CurPPLexer
->peekConditionalLevel();
345 const SourceLocation ConditionalBegin
= CurPPLexer
->getSourceLocation();
346 // If this is in a skipping block or if we're already handled this #if
347 // block, don't bother parsing the condition.
348 if (CondInfo
.WasSkipping
|| CondInfo
.FoundNonSkip
) {
349 DiscardUntilEndOfDirective();
352 // Restore the value of LexingRawMode so that identifiers are
353 // looked up, etc, inside the #elif expression.
354 assert(CurPPLexer
->LexingRawMode
&& "We have to be skipping here!");
355 CurPPLexer
->LexingRawMode
= false;
356 IdentifierInfo
*IfNDefMacro
= 0;
357 ShouldEnter
= EvaluateDirectiveExpression(IfNDefMacro
);
358 CurPPLexer
->LexingRawMode
= true;
360 const SourceLocation ConditionalEnd
= CurPPLexer
->getSourceLocation();
362 // If this is a #elif with a #else before it, report the error.
363 if (CondInfo
.FoundElse
) Diag(Tok
, diag::pp_err_elif_after_else
);
366 Callbacks
->Elif(SourceRange(ConditionalBegin
, ConditionalEnd
));
368 // If this condition is true, enter it!
370 CondInfo
.FoundNonSkip
= true;
376 CurPPLexer
->ParsingPreprocessorDirective
= false;
377 // Restore comment saving mode.
378 if (CurLexer
) CurLexer
->SetCommentRetentionState(KeepComments
);
381 // Finally, if we are out of the conditional (saw an #endif or ran off the end
382 // of the file, just stop skipping and return to lexing whatever came after
384 CurPPLexer
->LexingRawMode
= false;
387 void Preprocessor::PTHSkipExcludedConditionalBlock() {
391 assert(CurPTHLexer
->LexingRawMode
== false);
393 // Skip to the next '#else', '#elif', or #endif.
394 if (CurPTHLexer
->SkipBlock()) {
395 // We have reached an #endif. Both the '#' and 'endif' tokens
396 // have been consumed by the PTHLexer. Just pop off the condition level.
397 PPConditionalInfo CondInfo
;
398 bool InCond
= CurPTHLexer
->popConditionalLevel(CondInfo
);
399 (void)InCond
; // Silence warning in no-asserts mode.
400 assert(!InCond
&& "Can't be skipping if not in a conditional!");
404 // We have reached a '#else' or '#elif'. Lex the next token to get
405 // the directive flavor.
407 LexUnexpandedToken(Tok
);
409 // We can actually look up the IdentifierInfo here since we aren't in
411 tok::PPKeywordKind K
= Tok
.getIdentifierInfo()->getPPKeywordID();
413 if (K
== tok::pp_else
) {
414 // #else: Enter the else condition. We aren't in a nested condition
415 // since we skip those. We're always in the one matching the last
416 // blocked we skipped.
417 PPConditionalInfo
&CondInfo
= CurPTHLexer
->peekConditionalLevel();
418 // Note that we've seen a #else in this conditional.
419 CondInfo
.FoundElse
= true;
421 // If the #if block wasn't entered then enter the #else block now.
422 if (!CondInfo
.FoundNonSkip
) {
423 CondInfo
.FoundNonSkip
= true;
425 // Scan until the eom token.
426 CurPTHLexer
->ParsingPreprocessorDirective
= true;
427 DiscardUntilEndOfDirective();
428 CurPTHLexer
->ParsingPreprocessorDirective
= false;
433 // Otherwise skip this block.
437 assert(K
== tok::pp_elif
);
438 PPConditionalInfo
&CondInfo
= CurPTHLexer
->peekConditionalLevel();
440 // If this is a #elif with a #else before it, report the error.
441 if (CondInfo
.FoundElse
)
442 Diag(Tok
, diag::pp_err_elif_after_else
);
444 // If this is in a skipping block or if we're already handled this #if
445 // block, don't bother parsing the condition. We just skip this block.
446 if (CondInfo
.FoundNonSkip
)
449 // Evaluate the condition of the #elif.
450 IdentifierInfo
*IfNDefMacro
= 0;
451 CurPTHLexer
->ParsingPreprocessorDirective
= true;
452 bool ShouldEnter
= EvaluateDirectiveExpression(IfNDefMacro
);
453 CurPTHLexer
->ParsingPreprocessorDirective
= false;
455 // If this condition is true, enter it!
457 CondInfo
.FoundNonSkip
= true;
461 // Otherwise, skip this block and go to the next one.
466 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
467 /// return null on failure. isAngled indicates whether the file reference is
468 /// for system #include's or not (i.e. using <> instead of "").
469 const FileEntry
*Preprocessor::LookupFile(llvm::StringRef Filename
,
471 const DirectoryLookup
*FromDir
,
472 const DirectoryLookup
*&CurDir
) {
473 // If the header lookup mechanism may be relative to the current file, pass in
474 // info about where the current file is.
475 const FileEntry
*CurFileEnt
= 0;
477 FileID FID
= getCurrentFileLexer()->getFileID();
478 CurFileEnt
= SourceMgr
.getFileEntryForID(FID
);
480 // If there is no file entry associated with this file, it must be the
481 // predefines buffer. Any other file is not lexed with a normal lexer, so
482 // it won't be scanned for preprocessor directives. If we have the
483 // predefines buffer, resolve #include references (which come from the
484 // -include command line argument) as if they came from the main file, this
485 // affects file lookup etc.
486 if (CurFileEnt
== 0) {
487 FID
= SourceMgr
.getMainFileID();
488 CurFileEnt
= SourceMgr
.getFileEntryForID(FID
);
492 // Do a standard file entry lookup.
493 CurDir
= CurDirLookup
;
494 const FileEntry
*FE
=
495 HeaderInfo
.LookupFile(Filename
, isAngled
, FromDir
, CurDir
, CurFileEnt
);
498 // Otherwise, see if this is a subframework header. If so, this is relative
499 // to one of the headers on the #include stack. Walk the list of the current
500 // headers on the #include stack and pass them to HeaderInfo.
502 if ((CurFileEnt
= SourceMgr
.getFileEntryForID(CurPPLexer
->getFileID())))
503 if ((FE
= HeaderInfo
.LookupSubframeworkHeader(Filename
, CurFileEnt
)))
507 for (unsigned i
= 0, e
= IncludeMacroStack
.size(); i
!= e
; ++i
) {
508 IncludeStackInfo
&ISEntry
= IncludeMacroStack
[e
-i
-1];
509 if (IsFileLexer(ISEntry
)) {
511 SourceMgr
.getFileEntryForID(ISEntry
.ThePPLexer
->getFileID())))
512 if ((FE
= HeaderInfo
.LookupSubframeworkHeader(Filename
, CurFileEnt
)))
517 // Otherwise, we really couldn't find the file.
522 //===----------------------------------------------------------------------===//
523 // Preprocessor Directive Handling.
524 //===----------------------------------------------------------------------===//
526 /// HandleDirective - This callback is invoked when the lexer sees a # token
527 /// at the start of a line. This consumes the directive, modifies the
528 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
529 /// read is the correct one.
530 void Preprocessor::HandleDirective(Token
&Result
) {
531 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
533 // We just parsed a # character at the start of a line, so we're in directive
534 // mode. Tell the lexer this so any newlines we see will be converted into an
535 // EOM token (which terminates the directive).
536 CurPPLexer
->ParsingPreprocessorDirective
= true;
540 // We are about to read a token. For the multiple-include optimization FA to
541 // work, we have to remember if we had read any tokens *before* this
543 bool ReadAnyTokensBeforeDirective
=CurPPLexer
->MIOpt
.getHasReadAnyTokensVal();
545 // Save the '#' token in case we need to return it later.
546 Token SavedHash
= Result
;
548 // Read the next token, the directive flavor. This isn't expanded due to
550 LexUnexpandedToken(Result
);
552 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
557 // If so, the user is relying on non-portable behavior, emit a diagnostic.
559 Diag(Result
, diag::ext_embedded_directive
);
562 switch (Result
.getKind()) {
564 return; // null directive.
566 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
567 LexUnexpandedToken(Result
);
569 case tok::code_completion
:
571 CodeComplete
->CodeCompleteDirective(
572 CurPPLexer
->getConditionalStackDepth() > 0);
574 case tok::numeric_constant
: // # 7 GNU line marker directive.
575 if (getLangOptions().AsmPreprocessor
)
576 break; // # 4 is not a preprocessor directive in .S files.
577 return HandleDigitDirective(Result
);
579 IdentifierInfo
*II
= Result
.getIdentifierInfo();
580 if (II
== 0) break; // Not an identifier.
582 // Ask what the preprocessor keyword ID is.
583 switch (II
->getPPKeywordID()) {
585 // C99 6.10.1 - Conditional Inclusion.
587 return HandleIfDirective(Result
, ReadAnyTokensBeforeDirective
);
589 return HandleIfdefDirective(Result
, false, true/*not valid for miopt*/);
591 return HandleIfdefDirective(Result
, true, ReadAnyTokensBeforeDirective
);
593 return HandleElifDirective(Result
);
595 return HandleElseDirective(Result
);
597 return HandleEndifDirective(Result
);
599 // C99 6.10.2 - Source File Inclusion.
600 case tok::pp_include
:
602 return HandleIncludeDirective(SavedHash
.getLocation(), Result
);
603 case tok::pp___include_macros
:
605 return HandleIncludeMacrosDirective(SavedHash
.getLocation(), Result
);
607 // C99 6.10.3 - Macro Replacement.
609 return HandleDefineDirective(Result
);
611 return HandleUndefDirective(Result
);
613 // C99 6.10.4 - Line Control.
615 return HandleLineDirective(Result
);
617 // C99 6.10.5 - Error Directive.
619 return HandleUserDiagnosticDirective(Result
, false);
621 // C99 6.10.6 - Pragma Directive.
623 return HandlePragmaDirective(PIK_HashPragma
);
627 return HandleImportDirective(SavedHash
.getLocation(), Result
);
628 case tok::pp_include_next
:
629 return HandleIncludeNextDirective(SavedHash
.getLocation(), Result
);
631 case tok::pp_warning
:
632 Diag(Result
, diag::ext_pp_warning_directive
);
633 return HandleUserDiagnosticDirective(Result
, true);
635 return HandleIdentSCCSDirective(Result
);
637 return HandleIdentSCCSDirective(Result
);
639 //isExtension = true; // FIXME: implement #assert
641 case tok::pp_unassert
:
642 //isExtension = true; // FIXME: implement #unassert
648 // If this is a .S file, treat unknown # directives as non-preprocessor
649 // directives. This is important because # may be a comment or introduce
650 // various pseudo-ops. Just return the # token and push back the following
651 // token to be lexed next time.
652 if (getLangOptions().AsmPreprocessor
) {
653 Token
*Toks
= new Token
[2];
654 // Return the # and the token after it.
658 // If the second token is a hashhash token, then we need to translate it to
659 // unknown so the token lexer doesn't try to perform token pasting.
660 if (Result
.is(tok::hashhash
))
661 Toks
[1].setKind(tok::unknown
);
663 // Enter this token stream so that we re-lex the tokens. Make sure to
664 // enable macro expansion, in case the token after the # is an identifier
666 EnterTokenStream(Toks
, 2, false, true);
670 // If we reached here, the preprocessing token is not valid!
671 Diag(Result
, diag::err_pp_invalid_directive
);
673 // Read the rest of the PP line.
674 DiscardUntilEndOfDirective();
676 // Okay, we're done parsing the directive.
679 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
680 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
681 static bool GetLineValue(Token
&DigitTok
, unsigned &Val
,
682 unsigned DiagID
, Preprocessor
&PP
) {
683 if (DigitTok
.isNot(tok::numeric_constant
)) {
684 PP
.Diag(DigitTok
, DiagID
);
686 if (DigitTok
.isNot(tok::eom
))
687 PP
.DiscardUntilEndOfDirective();
691 llvm::SmallString
<64> IntegerBuffer
;
692 IntegerBuffer
.resize(DigitTok
.getLength());
693 const char *DigitTokBegin
= &IntegerBuffer
[0];
694 bool Invalid
= false;
695 unsigned ActualLength
= PP
.getSpelling(DigitTok
, DigitTokBegin
, &Invalid
);
699 // Verify that we have a simple digit-sequence, and compute the value. This
700 // is always a simple digit string computed in decimal, so we do this manually
703 for (unsigned i
= 0; i
!= ActualLength
; ++i
) {
704 if (!isdigit(DigitTokBegin
[i
])) {
705 PP
.Diag(PP
.AdvanceToTokenCharacter(DigitTok
.getLocation(), i
),
706 diag::err_pp_line_digit_sequence
);
707 PP
.DiscardUntilEndOfDirective();
711 unsigned NextVal
= Val
*10+(DigitTokBegin
[i
]-'0');
712 if (NextVal
< Val
) { // overflow.
713 PP
.Diag(DigitTok
, DiagID
);
714 PP
.DiscardUntilEndOfDirective();
720 // Reject 0, this is needed both by #line numbers and flags.
722 PP
.Diag(DigitTok
, DiagID
);
723 PP
.DiscardUntilEndOfDirective();
727 if (DigitTokBegin
[0] == '0')
728 PP
.Diag(DigitTok
.getLocation(), diag::warn_pp_line_decimal
);
733 /// HandleLineDirective - Handle #line directive: C99 6.10.4. The two
734 /// acceptable forms are:
735 /// # line digit-sequence
736 /// # line digit-sequence "s-char-sequence"
737 void Preprocessor::HandleLineDirective(Token
&Tok
) {
738 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
743 // Validate the number and convert it to an unsigned.
745 if (GetLineValue(DigitTok
, LineNo
, diag::err_pp_line_requires_integer
,*this))
748 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
749 // number greater than 2147483647". C90 requires that the line # be <= 32767.
750 unsigned LineLimit
= Features
.C99
? 2147483648U : 32768U;
751 if (LineNo
>= LineLimit
)
752 Diag(DigitTok
, diag::ext_pp_line_too_big
) << LineLimit
;
758 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a
759 // string followed by eom.
760 if (StrTok
.is(tok::eom
))
762 else if (StrTok
.isNot(tok::string_literal
)) {
763 Diag(StrTok
, diag::err_pp_line_invalid_filename
);
764 DiscardUntilEndOfDirective();
767 // Parse and validate the string, converting it into a unique ID.
768 StringLiteralParser
Literal(&StrTok
, 1, *this);
769 assert(!Literal
.AnyWide
&& "Didn't allow wide strings in");
770 if (Literal
.hadError
)
771 return DiscardUntilEndOfDirective();
772 if (Literal
.Pascal
) {
773 Diag(StrTok
, diag::err_pp_linemarker_invalid_filename
);
774 return DiscardUntilEndOfDirective();
776 FilenameID
= SourceMgr
.getLineTableFilenameID(Literal
.GetString(),
777 Literal
.GetStringLength());
779 // Verify that there is nothing after the string, other than EOM. Because
780 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
781 CheckEndOfDirective("line", true);
784 SourceMgr
.AddLineNote(DigitTok
.getLocation(), LineNo
, FilenameID
);
787 Callbacks
->FileChanged(CurPPLexer
->getSourceLocation(),
788 PPCallbacks::RenameFile
,
792 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
793 /// marker directive.
794 static bool ReadLineMarkerFlags(bool &IsFileEntry
, bool &IsFileExit
,
795 bool &IsSystemHeader
, bool &IsExternCHeader
,
800 if (FlagTok
.is(tok::eom
)) return false;
801 if (GetLineValue(FlagTok
, FlagVal
, diag::err_pp_linemarker_invalid_flag
, PP
))
808 if (FlagTok
.is(tok::eom
)) return false;
809 if (GetLineValue(FlagTok
, FlagVal
, diag::err_pp_linemarker_invalid_flag
,PP
))
811 } else if (FlagVal
== 2) {
814 SourceManager
&SM
= PP
.getSourceManager();
815 // If we are leaving the current presumed file, check to make sure the
816 // presumed include stack isn't empty!
818 SM
.getDecomposedInstantiationLoc(FlagTok
.getLocation()).first
;
819 PresumedLoc PLoc
= SM
.getPresumedLoc(FlagTok
.getLocation());
820 if (PLoc
.isInvalid())
823 // If there is no include loc (main file) or if the include loc is in a
824 // different physical file, then we aren't in a "1" line marker flag region.
825 SourceLocation IncLoc
= PLoc
.getIncludeLoc();
826 if (IncLoc
.isInvalid() ||
827 SM
.getDecomposedInstantiationLoc(IncLoc
).first
!= CurFileID
) {
828 PP
.Diag(FlagTok
, diag::err_pp_linemarker_invalid_pop
);
829 PP
.DiscardUntilEndOfDirective();
834 if (FlagTok
.is(tok::eom
)) return false;
835 if (GetLineValue(FlagTok
, FlagVal
, diag::err_pp_linemarker_invalid_flag
,PP
))
839 // We must have 3 if there are still flags.
841 PP
.Diag(FlagTok
, diag::err_pp_linemarker_invalid_flag
);
842 PP
.DiscardUntilEndOfDirective();
846 IsSystemHeader
= true;
849 if (FlagTok
.is(tok::eom
)) return false;
850 if (GetLineValue(FlagTok
, FlagVal
, diag::err_pp_linemarker_invalid_flag
, PP
))
853 // We must have 4 if there is yet another flag.
855 PP
.Diag(FlagTok
, diag::err_pp_linemarker_invalid_flag
);
856 PP
.DiscardUntilEndOfDirective();
860 IsExternCHeader
= true;
863 if (FlagTok
.is(tok::eom
)) return false;
865 // There are no more valid flags here.
866 PP
.Diag(FlagTok
, diag::err_pp_linemarker_invalid_flag
);
867 PP
.DiscardUntilEndOfDirective();
871 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
872 /// one of the following forms:
875 /// # 42 "file" ('1' | '2')?
876 /// # 42 "file" ('1' | '2')? '3' '4'?
878 void Preprocessor::HandleDigitDirective(Token
&DigitTok
) {
879 // Validate the number and convert it to an unsigned. GNU does not have a
880 // line # limit other than it fit in 32-bits.
882 if (GetLineValue(DigitTok
, LineNo
, diag::err_pp_linemarker_requires_integer
,
889 bool IsFileEntry
= false, IsFileExit
= false;
890 bool IsSystemHeader
= false, IsExternCHeader
= false;
893 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a
894 // string followed by eom.
895 if (StrTok
.is(tok::eom
))
897 else if (StrTok
.isNot(tok::string_literal
)) {
898 Diag(StrTok
, diag::err_pp_linemarker_invalid_filename
);
899 return DiscardUntilEndOfDirective();
901 // Parse and validate the string, converting it into a unique ID.
902 StringLiteralParser
Literal(&StrTok
, 1, *this);
903 assert(!Literal
.AnyWide
&& "Didn't allow wide strings in");
904 if (Literal
.hadError
)
905 return DiscardUntilEndOfDirective();
906 if (Literal
.Pascal
) {
907 Diag(StrTok
, diag::err_pp_linemarker_invalid_filename
);
908 return DiscardUntilEndOfDirective();
910 FilenameID
= SourceMgr
.getLineTableFilenameID(Literal
.GetString(),
911 Literal
.GetStringLength());
913 // If a filename was present, read any flags that are present.
914 if (ReadLineMarkerFlags(IsFileEntry
, IsFileExit
,
915 IsSystemHeader
, IsExternCHeader
, *this))
919 // Create a line note with this information.
920 SourceMgr
.AddLineNote(DigitTok
.getLocation(), LineNo
, FilenameID
,
921 IsFileEntry
, IsFileExit
,
922 IsSystemHeader
, IsExternCHeader
);
924 // If the preprocessor has callbacks installed, notify them of the #line
925 // change. This is used so that the line marker comes out in -E mode for
928 PPCallbacks::FileChangeReason Reason
= PPCallbacks::RenameFile
;
930 Reason
= PPCallbacks::EnterFile
;
932 Reason
= PPCallbacks::ExitFile
;
933 SrcMgr::CharacteristicKind FileKind
= SrcMgr::C_User
;
935 FileKind
= SrcMgr::C_ExternCSystem
;
936 else if (IsSystemHeader
)
937 FileKind
= SrcMgr::C_System
;
939 Callbacks
->FileChanged(CurPPLexer
->getSourceLocation(), Reason
, FileKind
);
944 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
946 void Preprocessor::HandleUserDiagnosticDirective(Token
&Tok
,
948 // PTH doesn't emit #warning or #error directives.
950 return CurPTHLexer
->DiscardToEndOfLine();
952 // Read the rest of the line raw. We do this because we don't want macros
953 // to be expanded and we don't require that the tokens be valid preprocessing
954 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
955 // collapse multiple consequtive white space between tokens, but this isn't
956 // specified by the standard.
957 std::string Message
= CurLexer
->ReadToEndOfLine();
959 Diag(Tok
, diag::pp_hash_warning
) << Message
;
961 Diag(Tok
, diag::err_pp_hash_error
) << Message
;
964 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
966 void Preprocessor::HandleIdentSCCSDirective(Token
&Tok
) {
967 // Yes, this directive is an extension.
968 Diag(Tok
, diag::ext_pp_ident_directive
);
970 // Read the string argument.
974 // If the token kind isn't a string, it's a malformed directive.
975 if (StrTok
.isNot(tok::string_literal
) &&
976 StrTok
.isNot(tok::wide_string_literal
)) {
977 Diag(StrTok
, diag::err_pp_malformed_ident
);
978 if (StrTok
.isNot(tok::eom
))
979 DiscardUntilEndOfDirective();
983 // Verify that there is nothing after the string, other than EOM.
984 CheckEndOfDirective("ident");
987 bool Invalid
= false;
988 std::string Str
= getSpelling(StrTok
, &Invalid
);
990 Callbacks
->Ident(Tok
.getLocation(), Str
);
994 //===----------------------------------------------------------------------===//
995 // Preprocessor Include Directive Handling.
996 //===----------------------------------------------------------------------===//
998 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
999 /// checked and spelled filename, e.g. as an operand of #include. This returns
1000 /// true if the input filename was in <>'s or false if it were in ""'s. The
1001 /// caller is expected to provide a buffer that is large enough to hold the
1002 /// spelling of the filename, but is also expected to handle the case when
1003 /// this method decides to use a different buffer.
1004 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc
,
1005 llvm::StringRef
&Buffer
) {
1006 // Get the text form of the filename.
1007 assert(!Buffer
.empty() && "Can't have tokens with empty spellings!");
1009 // Make sure the filename is <x> or "x".
1011 if (Buffer
[0] == '<') {
1012 if (Buffer
.back() != '>') {
1013 Diag(Loc
, diag::err_pp_expects_filename
);
1014 Buffer
= llvm::StringRef();
1018 } else if (Buffer
[0] == '"') {
1019 if (Buffer
.back() != '"') {
1020 Diag(Loc
, diag::err_pp_expects_filename
);
1021 Buffer
= llvm::StringRef();
1026 Diag(Loc
, diag::err_pp_expects_filename
);
1027 Buffer
= llvm::StringRef();
1031 // Diagnose #include "" as invalid.
1032 if (Buffer
.size() <= 2) {
1033 Diag(Loc
, diag::err_pp_empty_filename
);
1034 Buffer
= llvm::StringRef();
1038 // Skip the brackets.
1039 Buffer
= Buffer
.substr(1, Buffer
.size()-2);
1043 /// ConcatenateIncludeName - Handle cases where the #include name is expanded
1044 /// from a macro as multiple tokens, which need to be glued together. This
1045 /// occurs for code like:
1046 /// #define FOO <a/b.h>
1048 /// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1050 /// This code concatenates and consumes tokens up to the '>' token. It returns
1051 /// false if the > was found, otherwise it returns true if it finds and consumes
1053 bool Preprocessor::ConcatenateIncludeName(
1054 llvm::SmallString
<128> &FilenameBuffer
,
1055 SourceLocation
&End
) {
1059 while (CurTok
.isNot(tok::eom
)) {
1060 End
= CurTok
.getLocation();
1062 // FIXME: Provide code completion for #includes.
1063 if (CurTok
.is(tok::code_completion
)) {
1068 // Append the spelling of this token to the buffer. If there was a space
1069 // before it, add it now.
1070 if (CurTok
.hasLeadingSpace())
1071 FilenameBuffer
.push_back(' ');
1073 // Get the spelling of the token, directly into FilenameBuffer if possible.
1074 unsigned PreAppendSize
= FilenameBuffer
.size();
1075 FilenameBuffer
.resize(PreAppendSize
+CurTok
.getLength());
1077 const char *BufPtr
= &FilenameBuffer
[PreAppendSize
];
1078 unsigned ActualLen
= getSpelling(CurTok
, BufPtr
);
1080 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1081 if (BufPtr
!= &FilenameBuffer
[PreAppendSize
])
1082 memcpy(&FilenameBuffer
[PreAppendSize
], BufPtr
, ActualLen
);
1084 // Resize FilenameBuffer to the correct size.
1085 if (CurTok
.getLength() != ActualLen
)
1086 FilenameBuffer
.resize(PreAppendSize
+ActualLen
);
1088 // If we found the '>' marker, return success.
1089 if (CurTok
.is(tok::greater
))
1095 // If we hit the eom marker, emit an error and return true so that the caller
1096 // knows the EOM has been read.
1097 Diag(CurTok
.getLocation(), diag::err_pp_expects_filename
);
1101 /// HandleIncludeDirective - The "#include" tokens have just been read, read the
1102 /// file to be included from the lexer, then include it! This is a common
1103 /// routine with functionality shared between #include, #include_next and
1104 /// #import. LookupFrom is set when this is a #include_next directive, it
1105 /// specifies the file to start searching from.
1106 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc
,
1108 const DirectoryLookup
*LookupFrom
,
1112 CurPPLexer
->LexIncludeFilename(FilenameTok
);
1114 // Reserve a buffer to get the spelling.
1115 llvm::SmallString
<128> FilenameBuffer
;
1116 llvm::StringRef Filename
;
1119 switch (FilenameTok
.getKind()) {
1121 // If the token kind is EOM, the error has already been diagnosed.
1124 case tok::angle_string_literal
:
1125 case tok::string_literal
:
1126 Filename
= getSpelling(FilenameTok
, FilenameBuffer
);
1127 End
= FilenameTok
.getLocation();
1131 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1132 // case, glue the tokens together into FilenameBuffer and interpret those.
1133 FilenameBuffer
.push_back('<');
1134 if (ConcatenateIncludeName(FilenameBuffer
, End
))
1135 return; // Found <eom> but no ">"? Diagnostic already emitted.
1136 Filename
= FilenameBuffer
.str();
1139 Diag(FilenameTok
.getLocation(), diag::err_pp_expects_filename
);
1140 DiscardUntilEndOfDirective();
1145 GetIncludeFilenameSpelling(FilenameTok
.getLocation(), Filename
);
1146 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1148 if (Filename
.empty()) {
1149 DiscardUntilEndOfDirective();
1153 // Verify that there is nothing after the filename, other than EOM. Note that
1154 // we allow macros that expand to nothing after the filename, because this
1155 // falls into the category of "#include pp-tokens new-line" specified in
1157 CheckEndOfDirective(IncludeTok
.getIdentifierInfo()->getNameStart(), true);
1159 // Check that we don't have infinite #include recursion.
1160 if (IncludeMacroStack
.size() == MaxAllowedIncludeStackDepth
-1) {
1161 Diag(FilenameTok
, diag::err_pp_include_too_deep
);
1165 // Search include directories.
1166 const DirectoryLookup
*CurDir
;
1167 const FileEntry
*File
= LookupFile(Filename
, isAngled
, LookupFrom
, CurDir
);
1169 Diag(FilenameTok
, diag::err_pp_file_not_found
) << Filename
;
1173 // Notify the callback object that we've seen an inclusion directive.
1175 Callbacks
->InclusionDirective(HashLoc
, IncludeTok
, Filename
, isAngled
, File
,
1178 // The #included file will be considered to be a system header if either it is
1179 // in a system include directory, or if the #includer is a system include
1181 SrcMgr::CharacteristicKind FileCharacter
=
1182 std::max(HeaderInfo
.getFileDirFlavor(File
),
1183 SourceMgr
.getFileCharacteristic(FilenameTok
.getLocation()));
1185 // Ask HeaderInfo if we should enter this #include file. If not, #including
1186 // this file will have no effect.
1187 if (!HeaderInfo
.ShouldEnterIncludeFile(File
, isImport
)) {
1189 Callbacks
->FileSkipped(*File
, FilenameTok
, FileCharacter
);
1193 // Look up the file, create a File ID for it.
1194 FileID FID
= SourceMgr
.createFileID(File
, FilenameTok
.getLocation(),
1196 if (FID
.isInvalid()) {
1197 Diag(FilenameTok
, diag::err_pp_file_not_found
) << Filename
;
1201 // Finally, if all is good, enter the new file!
1202 EnterSourceFile(FID
, CurDir
, FilenameTok
.getLocation());
1205 /// HandleIncludeNextDirective - Implements #include_next.
1207 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc
,
1208 Token
&IncludeNextTok
) {
1209 Diag(IncludeNextTok
, diag::ext_pp_include_next_directive
);
1211 // #include_next is like #include, except that we start searching after
1212 // the current found directory. If we can't do this, issue a
1214 const DirectoryLookup
*Lookup
= CurDirLookup
;
1215 if (isInPrimaryFile()) {
1217 Diag(IncludeNextTok
, diag::pp_include_next_in_primary
);
1218 } else if (Lookup
== 0) {
1219 Diag(IncludeNextTok
, diag::pp_include_next_absolute_path
);
1221 // Start looking up in the next directory.
1225 return HandleIncludeDirective(HashLoc
, IncludeNextTok
, Lookup
);
1228 /// HandleImportDirective - Implements #import.
1230 void Preprocessor::HandleImportDirective(SourceLocation HashLoc
,
1232 if (!Features
.ObjC1
) // #import is standard for ObjC.
1233 Diag(ImportTok
, diag::ext_pp_import_directive
);
1235 return HandleIncludeDirective(HashLoc
, ImportTok
, 0, true);
1238 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1239 /// pseudo directive in the predefines buffer. This handles it by sucking all
1240 /// tokens through the preprocessor and discarding them (only keeping the side
1241 /// effects on the preprocessor).
1242 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc
,
1243 Token
&IncludeMacrosTok
) {
1244 // This directive should only occur in the predefines buffer. If not, emit an
1245 // error and reject it.
1246 SourceLocation Loc
= IncludeMacrosTok
.getLocation();
1247 if (strcmp(SourceMgr
.getBufferName(Loc
), "<built-in>") != 0) {
1248 Diag(IncludeMacrosTok
.getLocation(),
1249 diag::pp_include_macros_out_of_predefines
);
1250 DiscardUntilEndOfDirective();
1254 // Treat this as a normal #include for checking purposes. If this is
1255 // successful, it will push a new lexer onto the include stack.
1256 HandleIncludeDirective(HashLoc
, IncludeMacrosTok
, 0, false);
1261 assert(TmpTok
.isNot(tok::eof
) && "Didn't find end of -imacros!");
1262 } while (TmpTok
.isNot(tok::hashhash
));
1265 //===----------------------------------------------------------------------===//
1266 // Preprocessor Macro Directive Handling.
1267 //===----------------------------------------------------------------------===//
1269 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1270 /// definition has just been read. Lex the rest of the arguments and the
1271 /// closing ), updating MI with what we learn. Return true if an error occurs
1272 /// parsing the arg list.
1273 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo
*MI
) {
1274 llvm::SmallVector
<IdentifierInfo
*, 32> Arguments
;
1278 LexUnexpandedToken(Tok
);
1279 switch (Tok
.getKind()) {
1281 // Found the end of the argument list.
1282 if (Arguments
.empty()) // #define FOO()
1284 // Otherwise we have #define FOO(A,)
1285 Diag(Tok
, diag::err_pp_expected_ident_in_arg_list
);
1287 case tok::ellipsis
: // #define X(... -> C99 varargs
1288 // Warn if use of C99 feature in non-C99 mode.
1289 if (!Features
.C99
) Diag(Tok
, diag::ext_variadic_macro
);
1291 // Lex the token after the identifier.
1292 LexUnexpandedToken(Tok
);
1293 if (Tok
.isNot(tok::r_paren
)) {
1294 Diag(Tok
, diag::err_pp_missing_rparen_in_macro_def
);
1297 // Add the __VA_ARGS__ identifier as an argument.
1298 Arguments
.push_back(Ident__VA_ARGS__
);
1299 MI
->setIsC99Varargs();
1300 MI
->setArgumentList(&Arguments
[0], Arguments
.size(), BP
);
1302 case tok::eom
: // #define X(
1303 Diag(Tok
, diag::err_pp_missing_rparen_in_macro_def
);
1306 // Handle keywords and identifiers here to accept things like
1307 // #define Foo(for) for.
1308 IdentifierInfo
*II
= Tok
.getIdentifierInfo();
1311 Diag(Tok
, diag::err_pp_invalid_tok_in_arg_list
);
1315 // If this is already used as an argument, it is used multiple times (e.g.
1317 if (std::find(Arguments
.begin(), Arguments
.end(), II
) !=
1318 Arguments
.end()) { // C99 6.10.3p6
1319 Diag(Tok
, diag::err_pp_duplicate_name_in_arg_list
) << II
;
1323 // Add the argument to the macro info.
1324 Arguments
.push_back(II
);
1326 // Lex the token after the identifier.
1327 LexUnexpandedToken(Tok
);
1329 switch (Tok
.getKind()) {
1330 default: // #define X(A B
1331 Diag(Tok
, diag::err_pp_expected_comma_in_arg_list
);
1333 case tok::r_paren
: // #define X(A)
1334 MI
->setArgumentList(&Arguments
[0], Arguments
.size(), BP
);
1336 case tok::comma
: // #define X(A,
1338 case tok::ellipsis
: // #define X(A... -> GCC extension
1339 // Diagnose extension.
1340 Diag(Tok
, diag::ext_named_variadic_macro
);
1342 // Lex the token after the identifier.
1343 LexUnexpandedToken(Tok
);
1344 if (Tok
.isNot(tok::r_paren
)) {
1345 Diag(Tok
, diag::err_pp_missing_rparen_in_macro_def
);
1349 MI
->setIsGNUVarargs();
1350 MI
->setArgumentList(&Arguments
[0], Arguments
.size(), BP
);
1357 /// HandleDefineDirective - Implements #define. This consumes the entire macro
1358 /// line then lets the caller lex the next real token.
1359 void Preprocessor::HandleDefineDirective(Token
&DefineTok
) {
1363 ReadMacroName(MacroNameTok
, 1);
1365 // Error reading macro name? If so, diagnostic already issued.
1366 if (MacroNameTok
.is(tok::eom
))
1369 Token LastTok
= MacroNameTok
;
1371 // If we are supposed to keep comments in #defines, reenable comment saving
1373 if (CurLexer
) CurLexer
->SetCommentRetentionState(KeepMacroComments
);
1375 // Create the new macro.
1376 MacroInfo
*MI
= AllocateMacroInfo(MacroNameTok
.getLocation());
1379 LexUnexpandedToken(Tok
);
1381 // If this is a function-like macro definition, parse the argument list,
1382 // marking each of the identifiers as being used as macro arguments. Also,
1383 // check other constraints on the first token of the macro body.
1384 if (Tok
.is(tok::eom
)) {
1385 // If there is no body to this macro, we have no special handling here.
1386 } else if (Tok
.hasLeadingSpace()) {
1387 // This is a normal token with leading space. Clear the leading space
1388 // marker on the first token to get proper expansion.
1389 Tok
.clearFlag(Token::LeadingSpace
);
1390 } else if (Tok
.is(tok::l_paren
)) {
1391 // This is a function-like macro definition. Read the argument list.
1392 MI
->setIsFunctionLike();
1393 if (ReadMacroDefinitionArgList(MI
)) {
1395 ReleaseMacroInfo(MI
);
1396 // Throw away the rest of the line.
1397 if (CurPPLexer
->ParsingPreprocessorDirective
)
1398 DiscardUntilEndOfDirective();
1402 // If this is a definition of a variadic C99 function-like macro, not using
1403 // the GNU named varargs extension, enabled __VA_ARGS__.
1405 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1406 // This gets unpoisoned where it is allowed.
1407 assert(Ident__VA_ARGS__
->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1408 if (MI
->isC99Varargs())
1409 Ident__VA_ARGS__
->setIsPoisoned(false);
1411 // Read the first token after the arg list for down below.
1412 LexUnexpandedToken(Tok
);
1413 } else if (Features
.C99
) {
1414 // C99 requires whitespace between the macro definition and the body. Emit
1415 // a diagnostic for something like "#define X+".
1416 Diag(Tok
, diag::ext_c99_whitespace_required_after_macro_name
);
1418 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1419 // first character of a replacement list is not a character required by
1420 // subclause 5.2.1, then there shall be white-space separation between the
1421 // identifier and the replacement list.". 5.2.1 lists this set:
1422 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1423 // is irrelevant here.
1424 bool isInvalid
= false;
1425 if (Tok
.is(tok::at
)) // @ is not in the list above.
1427 else if (Tok
.is(tok::unknown
)) {
1428 // If we have an unknown token, it is something strange like "`". Since
1429 // all of valid characters would have lexed into a single character
1430 // token of some sort, we know this is not a valid case.
1434 Diag(Tok
, diag::ext_missing_whitespace_after_macro_name
);
1436 Diag(Tok
, diag::warn_missing_whitespace_after_macro_name
);
1439 if (!Tok
.is(tok::eom
))
1442 // Read the rest of the macro body.
1443 if (MI
->isObjectLike()) {
1444 // Object-like macros are very simple, just read their body.
1445 while (Tok
.isNot(tok::eom
)) {
1447 MI
->AddTokenToBody(Tok
);
1448 // Get the next token of the macro.
1449 LexUnexpandedToken(Tok
);
1453 // Otherwise, read the body of a function-like macro. While we are at it,
1454 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1455 // parameters in function-like macro expansions.
1456 while (Tok
.isNot(tok::eom
)) {
1459 if (Tok
.isNot(tok::hash
)) {
1460 MI
->AddTokenToBody(Tok
);
1462 // Get the next token of the macro.
1463 LexUnexpandedToken(Tok
);
1467 // Get the next token of the macro.
1468 LexUnexpandedToken(Tok
);
1470 // Check for a valid macro arg identifier.
1471 if (Tok
.getIdentifierInfo() == 0 ||
1472 MI
->getArgumentNum(Tok
.getIdentifierInfo()) == -1) {
1474 // If this is assembler-with-cpp mode, we accept random gibberish after
1475 // the '#' because '#' is often a comment character. However, change
1476 // the kind of the token to tok::unknown so that the preprocessor isn't
1478 if (getLangOptions().AsmPreprocessor
&& Tok
.isNot(tok::eom
)) {
1479 LastTok
.setKind(tok::unknown
);
1481 Diag(Tok
, diag::err_pp_stringize_not_parameter
);
1482 ReleaseMacroInfo(MI
);
1484 // Disable __VA_ARGS__ again.
1485 Ident__VA_ARGS__
->setIsPoisoned(true);
1490 // Things look ok, add the '#' and param name tokens to the macro.
1491 MI
->AddTokenToBody(LastTok
);
1492 MI
->AddTokenToBody(Tok
);
1495 // Get the next token of the macro.
1496 LexUnexpandedToken(Tok
);
1501 // Disable __VA_ARGS__ again.
1502 Ident__VA_ARGS__
->setIsPoisoned(true);
1504 // Check that there is no paste (##) operator at the begining or end of the
1505 // replacement list.
1506 unsigned NumTokens
= MI
->getNumTokens();
1507 if (NumTokens
!= 0) {
1508 if (MI
->getReplacementToken(0).is(tok::hashhash
)) {
1509 Diag(MI
->getReplacementToken(0), diag::err_paste_at_start
);
1510 ReleaseMacroInfo(MI
);
1513 if (MI
->getReplacementToken(NumTokens
-1).is(tok::hashhash
)) {
1514 Diag(MI
->getReplacementToken(NumTokens
-1), diag::err_paste_at_end
);
1515 ReleaseMacroInfo(MI
);
1520 MI
->setDefinitionEndLoc(LastTok
.getLocation());
1522 // Finally, if this identifier already had a macro defined for it, verify that
1523 // the macro bodies are identical and free the old definition.
1524 if (MacroInfo
*OtherMI
= getMacroInfo(MacroNameTok
.getIdentifierInfo())) {
1525 // It is very common for system headers to have tons of macro redefinitions
1526 // and for warnings to be disabled in system headers. If this is the case,
1527 // then don't bother calling MacroInfo::isIdenticalTo.
1528 if (!getDiagnostics().getSuppressSystemWarnings() ||
1529 !SourceMgr
.isInSystemHeader(DefineTok
.getLocation())) {
1530 if (!OtherMI
->isUsed() && OtherMI
->isWarnIfUnused())
1531 Diag(OtherMI
->getDefinitionLoc(), diag::pp_macro_not_used
);
1533 // Macros must be identical. This means all tokens and whitespace
1534 // separation must be the same. C99 6.10.3.2.
1535 if (!OtherMI
->isAllowRedefinitionsWithoutWarning() &&
1536 !MI
->isIdenticalTo(*OtherMI
, *this)) {
1537 Diag(MI
->getDefinitionLoc(), diag::ext_pp_macro_redef
)
1538 << MacroNameTok
.getIdentifierInfo();
1539 Diag(OtherMI
->getDefinitionLoc(), diag::note_previous_definition
);
1542 if (OtherMI
->isWarnIfUnused())
1543 WarnUnusedMacroLocs
.erase(OtherMI
->getDefinitionLoc());
1544 ReleaseMacroInfo(OtherMI
);
1547 setMacroInfo(MacroNameTok
.getIdentifierInfo(), MI
);
1549 assert(!MI
->isUsed());
1550 // If we need warning for not using the macro, add its location in the
1551 // warn-because-unused-macro set. If it gets used it will be removed from set.
1552 if (isInPrimaryFile() && // don't warn for include'd macros.
1553 Diags
->getDiagnosticLevel(diag::pp_macro_not_used
,
1554 MI
->getDefinitionLoc()) != Diagnostic::Ignored
) {
1555 MI
->setIsWarnIfUnused(true);
1556 WarnUnusedMacroLocs
.insert(MI
->getDefinitionLoc());
1559 // If the callbacks want to know, tell them about the macro definition.
1561 Callbacks
->MacroDefined(MacroNameTok
, MI
);
1564 /// HandleUndefDirective - Implements #undef.
1566 void Preprocessor::HandleUndefDirective(Token
&UndefTok
) {
1570 ReadMacroName(MacroNameTok
, 2);
1572 // Error reading macro name? If so, diagnostic already issued.
1573 if (MacroNameTok
.is(tok::eom
))
1576 // Check to see if this is the last token on the #undef line.
1577 CheckEndOfDirective("undef");
1579 // Okay, we finally have a valid identifier to undef.
1580 MacroInfo
*MI
= getMacroInfo(MacroNameTok
.getIdentifierInfo());
1582 // If the macro is not defined, this is a noop undef, just return.
1583 if (MI
== 0) return;
1586 Diag(MI
->getDefinitionLoc(), diag::pp_macro_not_used
);
1588 // If the callbacks want to know, tell them about the macro #undef.
1590 Callbacks
->MacroUndefined(MacroNameTok
, MI
);
1592 if (MI
->isWarnIfUnused())
1593 WarnUnusedMacroLocs
.erase(MI
->getDefinitionLoc());
1595 // Free macro definition.
1596 ReleaseMacroInfo(MI
);
1597 setMacroInfo(MacroNameTok
.getIdentifierInfo(), 0);
1601 //===----------------------------------------------------------------------===//
1602 // Preprocessor Conditional Directive Handling.
1603 //===----------------------------------------------------------------------===//
1605 /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1606 /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1607 /// if any tokens have been returned or pp-directives activated before this
1608 /// #ifndef has been lexed.
1610 void Preprocessor::HandleIfdefDirective(Token
&Result
, bool isIfndef
,
1611 bool ReadAnyTokensBeforeDirective
) {
1613 Token DirectiveTok
= Result
;
1616 ReadMacroName(MacroNameTok
);
1618 // Error reading macro name? If so, diagnostic already issued.
1619 if (MacroNameTok
.is(tok::eom
)) {
1620 // Skip code until we get to #endif. This helps with recovery by not
1621 // emitting an error when the #endif is reached.
1622 SkipExcludedConditionalBlock(DirectiveTok
.getLocation(),
1623 /*Foundnonskip*/false, /*FoundElse*/false);
1627 // Check to see if this is the last token on the #if[n]def line.
1628 CheckEndOfDirective(isIfndef
? "ifndef" : "ifdef");
1630 IdentifierInfo
*MII
= MacroNameTok
.getIdentifierInfo();
1631 MacroInfo
*MI
= getMacroInfo(MII
);
1633 if (CurPPLexer
->getConditionalStackDepth() == 0) {
1634 // If the start of a top-level #ifdef and if the macro is not defined,
1635 // inform MIOpt that this might be the start of a proper include guard.
1636 // Otherwise it is some other form of unknown conditional which we can't
1638 if (!ReadAnyTokensBeforeDirective
&& MI
== 0) {
1639 assert(isIfndef
&& "#ifdef shouldn't reach here");
1640 CurPPLexer
->MIOpt
.EnterTopLevelIFNDEF(MII
);
1642 CurPPLexer
->MIOpt
.EnterTopLevelConditional();
1645 // If there is a macro, process it.
1646 if (MI
) // Mark it used.
1647 markMacroAsUsed(MI
);
1649 // Should we include the stuff contained by this directive?
1650 if (!MI
== isIfndef
) {
1651 // Yes, remember that we are inside a conditional, then lex the next token.
1652 CurPPLexer
->pushConditionalLevel(DirectiveTok
.getLocation(),
1653 /*wasskip*/false, /*foundnonskip*/true,
1654 /*foundelse*/false);
1656 // No, skip the contents of this block.
1657 SkipExcludedConditionalBlock(DirectiveTok
.getLocation(),
1658 /*Foundnonskip*/false,
1659 /*FoundElse*/false);
1664 Callbacks
->Ifndef(MacroNameTok
);
1666 Callbacks
->Ifdef(MacroNameTok
);
1670 /// HandleIfDirective - Implements the #if directive.
1672 void Preprocessor::HandleIfDirective(Token
&IfToken
,
1673 bool ReadAnyTokensBeforeDirective
) {
1676 // Parse and evaluate the conditional expression.
1677 IdentifierInfo
*IfNDefMacro
= 0;
1678 const SourceLocation ConditionalBegin
= CurPPLexer
->getSourceLocation();
1679 const bool ConditionalTrue
= EvaluateDirectiveExpression(IfNDefMacro
);
1680 const SourceLocation ConditionalEnd
= CurPPLexer
->getSourceLocation();
1682 // If this condition is equivalent to #ifndef X, and if this is the first
1683 // directive seen, handle it for the multiple-include optimization.
1684 if (CurPPLexer
->getConditionalStackDepth() == 0) {
1685 if (!ReadAnyTokensBeforeDirective
&& IfNDefMacro
&& ConditionalTrue
)
1686 CurPPLexer
->MIOpt
.EnterTopLevelIFNDEF(IfNDefMacro
);
1688 CurPPLexer
->MIOpt
.EnterTopLevelConditional();
1691 // Should we include the stuff contained by this directive?
1692 if (ConditionalTrue
) {
1693 // Yes, remember that we are inside a conditional, then lex the next token.
1694 CurPPLexer
->pushConditionalLevel(IfToken
.getLocation(), /*wasskip*/false,
1695 /*foundnonskip*/true, /*foundelse*/false);
1697 // No, skip the contents of this block.
1698 SkipExcludedConditionalBlock(IfToken
.getLocation(), /*Foundnonskip*/false,
1699 /*FoundElse*/false);
1703 Callbacks
->If(SourceRange(ConditionalBegin
, ConditionalEnd
));
1706 /// HandleEndifDirective - Implements the #endif directive.
1708 void Preprocessor::HandleEndifDirective(Token
&EndifToken
) {
1711 // Check that this is the whole directive.
1712 CheckEndOfDirective("endif");
1714 PPConditionalInfo CondInfo
;
1715 if (CurPPLexer
->popConditionalLevel(CondInfo
)) {
1716 // No conditionals on the stack: this is an #endif without an #if.
1717 Diag(EndifToken
, diag::err_pp_endif_without_if
);
1721 // If this the end of a top-level #endif, inform MIOpt.
1722 if (CurPPLexer
->getConditionalStackDepth() == 0)
1723 CurPPLexer
->MIOpt
.ExitTopLevelConditional();
1725 assert(!CondInfo
.WasSkipping
&& !CurPPLexer
->LexingRawMode
&&
1726 "This code should only be reachable in the non-skipping case!");
1732 /// HandleElseDirective - Implements the #else directive.
1734 void Preprocessor::HandleElseDirective(Token
&Result
) {
1737 // #else directive in a non-skipping conditional... start skipping.
1738 CheckEndOfDirective("else");
1740 PPConditionalInfo CI
;
1741 if (CurPPLexer
->popConditionalLevel(CI
)) {
1742 Diag(Result
, diag::pp_err_else_without_if
);
1746 // If this is a top-level #else, inform the MIOpt.
1747 if (CurPPLexer
->getConditionalStackDepth() == 0)
1748 CurPPLexer
->MIOpt
.EnterTopLevelConditional();
1750 // If this is a #else with a #else before it, report the error.
1751 if (CI
.FoundElse
) Diag(Result
, diag::pp_err_else_after_else
);
1753 // Finally, skip the rest of the contents of this block.
1754 SkipExcludedConditionalBlock(CI
.IfLoc
, /*Foundnonskip*/true,
1761 /// HandleElifDirective - Implements the #elif directive.
1763 void Preprocessor::HandleElifDirective(Token
&ElifToken
) {
1766 // #elif directive in a non-skipping conditional... start skipping.
1767 // We don't care what the condition is, because we will always skip it (since
1768 // the block immediately before it was included).
1769 const SourceLocation ConditionalBegin
= CurPPLexer
->getSourceLocation();
1770 DiscardUntilEndOfDirective();
1771 const SourceLocation ConditionalEnd
= CurPPLexer
->getSourceLocation();
1773 PPConditionalInfo CI
;
1774 if (CurPPLexer
->popConditionalLevel(CI
)) {
1775 Diag(ElifToken
, diag::pp_err_elif_without_if
);
1779 // If this is a top-level #elif, inform the MIOpt.
1780 if (CurPPLexer
->getConditionalStackDepth() == 0)
1781 CurPPLexer
->MIOpt
.EnterTopLevelConditional();
1783 // If this is a #elif with a #else before it, report the error.
1784 if (CI
.FoundElse
) Diag(ElifToken
, diag::pp_err_elif_after_else
);
1786 // Finally, skip the rest of the contents of this block.
1787 SkipExcludedConditionalBlock(CI
.IfLoc
, /*Foundnonskip*/true,
1788 /*FoundElse*/CI
.FoundElse
);
1791 Callbacks
->Elif(SourceRange(ConditionalBegin
, ConditionalEnd
));