Don't warn for -Wnon-virtual-dtor for dependent classes.
[clang.git] / lib / Lex / PPDirectives.cpp
blob0f0d25b887afc966299195025f6db2ad1af16b7a
1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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;
33 if (MICache) {
34 MIChain = MICache;
35 MICache = MICache->Next;
37 else {
38 MIChain = BP.Allocate<MacroInfoChain>();
41 MIChain->Next = MIChainHead;
42 MIChain->Prev = 0;
43 if (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);
53 return MI;
56 MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) {
57 MacroInfo *MI = AllocateMacroInfo();
58 new (MI) MacroInfo(MacroToClone, BP);
59 return MI;
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;
68 Prev->Next = Next;
69 if (Next)
70 Next->Prev = Prev;
72 else {
73 assert(MIChainHead == MIChain);
74 MIChainHead = MIChain->Next;
75 MIChainHead->Prev = 0;
77 MIChain->Next = MICache;
78 MICache = MIChain;
80 MI->Destroy();
83 /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
84 /// current line until the tok::eom token is found.
85 void Preprocessor::DiscardUntilEndOfDirective() {
86 Token Tmp;
87 do {
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)) {
102 if (CodeComplete)
103 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
104 LexUnexpandedToken(MacroNameTok);
105 return;
108 // Missing macro name?
109 if (MacroNameTok.is(tok::eom)) {
110 Diag(MacroNameTok, diag::err_pp_missing_macro_name);
111 return;
114 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
115 if (II == 0) {
116 bool Invalid = false;
117 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
118 if (Invalid)
119 return;
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;
126 else
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);
137 else
138 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
139 } else {
140 // Okay, we got a good identifier node. Return it.
141 return;
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) {
154 Token Tmp;
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.
158 if (EnableMacros)
159 Lex(Tmp);
160 else
161 LexUnexpandedToken(Tmp);
163 // There should be no tokens after the directive, but we allow them as an
164 // extension.
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.
172 FixItHint Hint;
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,
192 bool FoundElse) {
193 ++NumSkipped;
194 assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?");
196 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
197 FoundNonSkipPortion, FoundElse);
199 if (CurPTHLexer) {
200 PTHSkipExcludedConditionalBlock();
201 return;
204 // Enter raw mode to disable identifier lookup (and thus macro expansion),
205 // disabling warnings, etc.
206 CurPPLexer->LexingRawMode = true;
207 Token Tok;
208 while (1) {
209 CurLexer->Lex(Tok);
211 if (Tok.is(tok::code_completion)) {
212 if (CodeComplete)
213 CodeComplete->CodeCompleteInConditionalExclusion();
214 continue;
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
220 // the current one.
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.
229 break;
232 // If this token is not a preprocessor directive, just skip it.
233 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
234 continue;
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);
252 continue;
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);
268 continue;
271 // Get the identifier name without trigraphs or embedded newlines. Note
272 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
273 // when skipping.
274 char DirectiveBuf[20];
275 llvm::StringRef Directive;
276 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
277 Directive = llvm::StringRef(RawCharData, Tok.getLength());
278 } else {
279 std::string DirectiveStr = getSpelling(Tok);
280 unsigned IdLen = DirectiveStr.size();
281 if (IdLen >= 20) {
282 CurPPLexer->ParsingPreprocessorDirective = false;
283 // Restore comment saving mode.
284 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
285 continue;
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,
301 /*foundelse*/false);
303 if (Callbacks)
304 Callbacks->Endif();
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)
318 break;
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;
332 if (Callbacks)
333 Callbacks->Else();
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;
339 break;
341 } else if (Sub == "lif") { // "elif".
342 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
344 bool ShouldEnter;
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();
350 ShouldEnter = false;
351 } else {
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);
365 if (Callbacks)
366 Callbacks->Elif(SourceRange(ConditionalBegin, ConditionalEnd));
368 // If this condition is true, enter it!
369 if (ShouldEnter) {
370 CondInfo.FoundNonSkip = true;
371 break;
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
383 // the #if block.
384 CurPPLexer->LexingRawMode = false;
387 void Preprocessor::PTHSkipExcludedConditionalBlock() {
389 while (1) {
390 assert(CurPTHLexer);
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!");
401 break;
404 // We have reached a '#else' or '#elif'. Lex the next token to get
405 // the directive flavor.
406 Token Tok;
407 LexUnexpandedToken(Tok);
409 // We can actually look up the IdentifierInfo here since we aren't in
410 // raw mode.
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;
430 break;
433 // Otherwise skip this block.
434 continue;
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)
447 continue;
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!
456 if (ShouldEnter) {
457 CondInfo.FoundNonSkip = true;
458 break;
461 // Otherwise, skip this block and go to the next one.
462 continue;
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,
470 bool isAngled,
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;
476 if (!FromDir) {
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);
496 if (FE) return FE;
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.
501 if (IsFileLexer()) {
502 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
503 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
504 return FE;
507 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
508 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
509 if (IsFileLexer(ISEntry)) {
510 if ((CurFileEnt =
511 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
512 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
513 return FE;
517 // Otherwise, we really couldn't find the file.
518 return 0;
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;
538 ++NumDirectives;
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
542 // pp-directive.
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
549 // C99 6.10.3p8.
550 LexUnexpandedToken(Result);
552 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
553 // #define A(x) #x
554 // A(abc
555 // #warning blah
556 // def)
557 // If so, the user is relying on non-portable behavior, emit a diagnostic.
558 if (InMacroArgs)
559 Diag(Result, diag::ext_embedded_directive);
561 TryAgain:
562 switch (Result.getKind()) {
563 case tok::eom:
564 return; // null directive.
565 case tok::comment:
566 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
567 LexUnexpandedToken(Result);
568 goto TryAgain;
569 case tok::code_completion:
570 if (CodeComplete)
571 CodeComplete->CodeCompleteDirective(
572 CurPPLexer->getConditionalStackDepth() > 0);
573 return;
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);
578 default:
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()) {
584 default: break;
585 // C99 6.10.1 - Conditional Inclusion.
586 case tok::pp_if:
587 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
588 case tok::pp_ifdef:
589 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
590 case tok::pp_ifndef:
591 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
592 case tok::pp_elif:
593 return HandleElifDirective(Result);
594 case tok::pp_else:
595 return HandleElseDirective(Result);
596 case tok::pp_endif:
597 return HandleEndifDirective(Result);
599 // C99 6.10.2 - Source File Inclusion.
600 case tok::pp_include:
601 // Handle #include.
602 return HandleIncludeDirective(SavedHash.getLocation(), Result);
603 case tok::pp___include_macros:
604 // Handle -imacros.
605 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
607 // C99 6.10.3 - Macro Replacement.
608 case tok::pp_define:
609 return HandleDefineDirective(Result);
610 case tok::pp_undef:
611 return HandleUndefDirective(Result);
613 // C99 6.10.4 - Line Control.
614 case tok::pp_line:
615 return HandleLineDirective(Result);
617 // C99 6.10.5 - Error Directive.
618 case tok::pp_error:
619 return HandleUserDiagnosticDirective(Result, false);
621 // C99 6.10.6 - Pragma Directive.
622 case tok::pp_pragma:
623 return HandlePragmaDirective(PIK_HashPragma);
625 // GNU Extensions.
626 case tok::pp_import:
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);
634 case tok::pp_ident:
635 return HandleIdentSCCSDirective(Result);
636 case tok::pp_sccs:
637 return HandleIdentSCCSDirective(Result);
638 case tok::pp_assert:
639 //isExtension = true; // FIXME: implement #assert
640 break;
641 case tok::pp_unassert:
642 //isExtension = true; // FIXME: implement #unassert
643 break;
645 break;
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.
655 Toks[0] = SavedHash;
656 Toks[1] = Result;
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
665 // that is expanded.
666 EnterTokenStream(Toks, 2, false, true);
667 return;
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();
688 return true;
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);
696 if (Invalid)
697 return true;
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
701 // here.
702 Val = 0;
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();
708 return true;
711 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
712 if (NextVal < Val) { // overflow.
713 PP.Diag(DigitTok, DiagID);
714 PP.DiscardUntilEndOfDirective();
715 return true;
717 Val = NextVal;
720 // Reject 0, this is needed both by #line numbers and flags.
721 if (Val == 0) {
722 PP.Diag(DigitTok, DiagID);
723 PP.DiscardUntilEndOfDirective();
724 return true;
727 if (DigitTokBegin[0] == '0')
728 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal);
730 return false;
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
739 // expanded.
740 Token DigitTok;
741 Lex(DigitTok);
743 // Validate the number and convert it to an unsigned.
744 unsigned LineNo;
745 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
746 return;
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;
754 int FilenameID = -1;
755 Token StrTok;
756 Lex(StrTok);
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))
761 ; // ok
762 else if (StrTok.isNot(tok::string_literal)) {
763 Diag(StrTok, diag::err_pp_line_invalid_filename);
764 DiscardUntilEndOfDirective();
765 return;
766 } else {
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);
786 if (Callbacks)
787 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
788 PPCallbacks::RenameFile,
789 SrcMgr::C_User);
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,
796 Preprocessor &PP) {
797 unsigned FlagVal;
798 Token FlagTok;
799 PP.Lex(FlagTok);
800 if (FlagTok.is(tok::eom)) return false;
801 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
802 return true;
804 if (FlagVal == 1) {
805 IsFileEntry = true;
807 PP.Lex(FlagTok);
808 if (FlagTok.is(tok::eom)) return false;
809 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
810 return true;
811 } else if (FlagVal == 2) {
812 IsFileExit = true;
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!
817 FileID CurFileID =
818 SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first;
819 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
820 if (PLoc.isInvalid())
821 return true;
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();
830 return true;
833 PP.Lex(FlagTok);
834 if (FlagTok.is(tok::eom)) return false;
835 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
836 return true;
839 // We must have 3 if there are still flags.
840 if (FlagVal != 3) {
841 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
842 PP.DiscardUntilEndOfDirective();
843 return true;
846 IsSystemHeader = true;
848 PP.Lex(FlagTok);
849 if (FlagTok.is(tok::eom)) return false;
850 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
851 return true;
853 // We must have 4 if there is yet another flag.
854 if (FlagVal != 4) {
855 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
856 PP.DiscardUntilEndOfDirective();
857 return true;
860 IsExternCHeader = true;
862 PP.Lex(FlagTok);
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();
868 return true;
871 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
872 /// one of the following forms:
874 /// # 42
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.
881 unsigned LineNo;
882 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
883 *this))
884 return;
886 Token StrTok;
887 Lex(StrTok);
889 bool IsFileEntry = false, IsFileExit = false;
890 bool IsSystemHeader = false, IsExternCHeader = false;
891 int FilenameID = -1;
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))
896 ; // ok
897 else if (StrTok.isNot(tok::string_literal)) {
898 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
899 return DiscardUntilEndOfDirective();
900 } else {
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))
916 return;
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
926 // example.
927 if (Callbacks) {
928 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
929 if (IsFileEntry)
930 Reason = PPCallbacks::EnterFile;
931 else if (IsFileExit)
932 Reason = PPCallbacks::ExitFile;
933 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
934 if (IsExternCHeader)
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,
947 bool isWarning) {
948 // PTH doesn't emit #warning or #error directives.
949 if (CurPTHLexer)
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();
958 if (isWarning)
959 Diag(Tok, diag::pp_hash_warning) << Message;
960 else
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.
971 Token StrTok;
972 Lex(StrTok);
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();
980 return;
983 // Verify that there is nothing after the string, other than EOM.
984 CheckEndOfDirective("ident");
986 if (Callbacks) {
987 bool Invalid = false;
988 std::string Str = getSpelling(StrTok, &Invalid);
989 if (!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".
1010 bool isAngled;
1011 if (Buffer[0] == '<') {
1012 if (Buffer.back() != '>') {
1013 Diag(Loc, diag::err_pp_expects_filename);
1014 Buffer = llvm::StringRef();
1015 return true;
1017 isAngled = true;
1018 } else if (Buffer[0] == '"') {
1019 if (Buffer.back() != '"') {
1020 Diag(Loc, diag::err_pp_expects_filename);
1021 Buffer = llvm::StringRef();
1022 return true;
1024 isAngled = false;
1025 } else {
1026 Diag(Loc, diag::err_pp_expects_filename);
1027 Buffer = llvm::StringRef();
1028 return true;
1031 // Diagnose #include "" as invalid.
1032 if (Buffer.size() <= 2) {
1033 Diag(Loc, diag::err_pp_empty_filename);
1034 Buffer = llvm::StringRef();
1035 return true;
1038 // Skip the brackets.
1039 Buffer = Buffer.substr(1, Buffer.size()-2);
1040 return isAngled;
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>
1047 /// #include FOO
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
1052 /// the EOM marker.
1053 bool Preprocessor::ConcatenateIncludeName(
1054 llvm::SmallString<128> &FilenameBuffer,
1055 SourceLocation &End) {
1056 Token CurTok;
1058 Lex(CurTok);
1059 while (CurTok.isNot(tok::eom)) {
1060 End = CurTok.getLocation();
1062 // FIXME: Provide code completion for #includes.
1063 if (CurTok.is(tok::code_completion)) {
1064 Lex(CurTok);
1065 continue;
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))
1090 return false;
1092 Lex(CurTok);
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);
1098 return true;
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,
1107 Token &IncludeTok,
1108 const DirectoryLookup *LookupFrom,
1109 bool isImport) {
1111 Token FilenameTok;
1112 CurPPLexer->LexIncludeFilename(FilenameTok);
1114 // Reserve a buffer to get the spelling.
1115 llvm::SmallString<128> FilenameBuffer;
1116 llvm::StringRef Filename;
1117 SourceLocation End;
1119 switch (FilenameTok.getKind()) {
1120 case tok::eom:
1121 // If the token kind is EOM, the error has already been diagnosed.
1122 return;
1124 case tok::angle_string_literal:
1125 case tok::string_literal:
1126 Filename = getSpelling(FilenameTok, FilenameBuffer);
1127 End = FilenameTok.getLocation();
1128 break;
1130 case tok::less:
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();
1137 break;
1138 default:
1139 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1140 DiscardUntilEndOfDirective();
1141 return;
1144 bool isAngled =
1145 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1146 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1147 // error.
1148 if (Filename.empty()) {
1149 DiscardUntilEndOfDirective();
1150 return;
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
1156 // C99 6.10.2p4.
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);
1162 return;
1165 // Search include directories.
1166 const DirectoryLookup *CurDir;
1167 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
1168 if (File == 0) {
1169 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1170 return;
1173 // Notify the callback object that we've seen an inclusion directive.
1174 if (Callbacks)
1175 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, File,
1176 End);
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
1180 // header.
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)) {
1188 if (Callbacks)
1189 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1190 return;
1193 // Look up the file, create a File ID for it.
1194 FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(),
1195 FileCharacter);
1196 if (FID.isInvalid()) {
1197 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1198 return;
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
1213 // diagnostic.
1214 const DirectoryLookup *Lookup = CurDirLookup;
1215 if (isInPrimaryFile()) {
1216 Lookup = 0;
1217 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1218 } else if (Lookup == 0) {
1219 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1220 } else {
1221 // Start looking up in the next directory.
1222 ++Lookup;
1225 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
1228 /// HandleImportDirective - Implements #import.
1230 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1231 Token &ImportTok) {
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();
1251 return;
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);
1258 Token TmpTok;
1259 do {
1260 Lex(TmpTok);
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;
1276 Token Tok;
1277 while (1) {
1278 LexUnexpandedToken(Tok);
1279 switch (Tok.getKind()) {
1280 case tok::r_paren:
1281 // Found the end of the argument list.
1282 if (Arguments.empty()) // #define FOO()
1283 return false;
1284 // Otherwise we have #define FOO(A,)
1285 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1286 return true;
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);
1295 return true;
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);
1301 return false;
1302 case tok::eom: // #define X(
1303 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1304 return true;
1305 default:
1306 // Handle keywords and identifiers here to accept things like
1307 // #define Foo(for) for.
1308 IdentifierInfo *II = Tok.getIdentifierInfo();
1309 if (II == 0) {
1310 // #define X(1
1311 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1312 return true;
1315 // If this is already used as an argument, it is used multiple times (e.g.
1316 // #define X(A,A.
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;
1320 return true;
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);
1332 return true;
1333 case tok::r_paren: // #define X(A)
1334 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1335 return false;
1336 case tok::comma: // #define X(A,
1337 break;
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);
1346 return true;
1349 MI->setIsGNUVarargs();
1350 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1351 return false;
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) {
1360 ++NumDefined;
1362 Token MacroNameTok;
1363 ReadMacroName(MacroNameTok, 1);
1365 // Error reading macro name? If so, diagnostic already issued.
1366 if (MacroNameTok.is(tok::eom))
1367 return;
1369 Token LastTok = MacroNameTok;
1371 // If we are supposed to keep comments in #defines, reenable comment saving
1372 // mode.
1373 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
1375 // Create the new macro.
1376 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
1378 Token Tok;
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)) {
1394 // Forget about MI.
1395 ReleaseMacroInfo(MI);
1396 // Throw away the rest of the line.
1397 if (CurPPLexer->ParsingPreprocessorDirective)
1398 DiscardUntilEndOfDirective();
1399 return;
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);
1417 } else {
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.
1426 isInvalid = true;
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.
1431 isInvalid = true;
1433 if (isInvalid)
1434 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1435 else
1436 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
1439 if (!Tok.is(tok::eom))
1440 LastTok = Tok;
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)) {
1446 LastTok = Tok;
1447 MI->AddTokenToBody(Tok);
1448 // Get the next token of the macro.
1449 LexUnexpandedToken(Tok);
1452 } else {
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)) {
1457 LastTok = Tok;
1459 if (Tok.isNot(tok::hash)) {
1460 MI->AddTokenToBody(Tok);
1462 // Get the next token of the macro.
1463 LexUnexpandedToken(Tok);
1464 continue;
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
1477 // confused.
1478 if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eom)) {
1479 LastTok.setKind(tok::unknown);
1480 } else {
1481 Diag(Tok, diag::err_pp_stringize_not_parameter);
1482 ReleaseMacroInfo(MI);
1484 // Disable __VA_ARGS__ again.
1485 Ident__VA_ARGS__->setIsPoisoned(true);
1486 return;
1490 // Things look ok, add the '#' and param name tokens to the macro.
1491 MI->AddTokenToBody(LastTok);
1492 MI->AddTokenToBody(Tok);
1493 LastTok = 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);
1511 return;
1513 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
1514 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
1515 ReleaseMacroInfo(MI);
1516 return;
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.
1560 if (Callbacks)
1561 Callbacks->MacroDefined(MacroNameTok, MI);
1564 /// HandleUndefDirective - Implements #undef.
1566 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
1567 ++NumUndefined;
1569 Token MacroNameTok;
1570 ReadMacroName(MacroNameTok, 2);
1572 // Error reading macro name? If so, diagnostic already issued.
1573 if (MacroNameTok.is(tok::eom))
1574 return;
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;
1585 if (!MI->isUsed())
1586 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
1588 // If the callbacks want to know, tell them about the macro #undef.
1589 if (Callbacks)
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) {
1612 ++NumIf;
1613 Token DirectiveTok = Result;
1615 Token MacroNameTok;
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);
1624 return;
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
1637 // handle.
1638 if (!ReadAnyTokensBeforeDirective && MI == 0) {
1639 assert(isIfndef && "#ifdef shouldn't reach here");
1640 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII);
1641 } else
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);
1655 } else {
1656 // No, skip the contents of this block.
1657 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1658 /*Foundnonskip*/false,
1659 /*FoundElse*/false);
1662 if (Callbacks) {
1663 if (isIfndef)
1664 Callbacks->Ifndef(MacroNameTok);
1665 else
1666 Callbacks->Ifdef(MacroNameTok);
1670 /// HandleIfDirective - Implements the #if directive.
1672 void Preprocessor::HandleIfDirective(Token &IfToken,
1673 bool ReadAnyTokensBeforeDirective) {
1674 ++NumIf;
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);
1687 else
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);
1696 } else {
1697 // No, skip the contents of this block.
1698 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
1699 /*FoundElse*/false);
1702 if (Callbacks)
1703 Callbacks->If(SourceRange(ConditionalBegin, ConditionalEnd));
1706 /// HandleEndifDirective - Implements the #endif directive.
1708 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
1709 ++NumEndif;
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);
1718 return;
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!");
1728 if (Callbacks)
1729 Callbacks->Endif();
1732 /// HandleElseDirective - Implements the #else directive.
1734 void Preprocessor::HandleElseDirective(Token &Result) {
1735 ++NumElse;
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);
1743 return;
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,
1755 /*FoundElse*/true);
1757 if (Callbacks)
1758 Callbacks->Else();
1761 /// HandleElifDirective - Implements the #elif directive.
1763 void Preprocessor::HandleElifDirective(Token &ElifToken) {
1764 ++NumElse;
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);
1776 return;
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);
1790 if (Callbacks)
1791 Callbacks->Elif(SourceRange(ConditionalBegin, ConditionalEnd));