blob: b0e784bcd96a1a311d84b3aa9818185d57ef044e [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001//===--- 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.
11//
12//===----------------------------------------------------------------------===//
13
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/Basic/FileManager.h"
20#include "clang/Basic/SourceManager.h"
21#include "llvm/ADT/APInt.h"
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Utility Methods for Preprocessor Directive Handling.
26//===----------------------------------------------------------------------===//
27
28MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
29 MacroInfo *MI;
30
31 if (!MICache.empty()) {
32 MI = MICache.back();
33 MICache.pop_back();
34 } else
35 MI = (MacroInfo*) BP.Allocate<MacroInfo>();
36 new (MI) MacroInfo(L);
37 return MI;
38}
39
40/// ReleaseMacroInfo - Release the specified MacroInfo. This memory will
41/// be reused for allocating new MacroInfo objects.
42void Preprocessor::ReleaseMacroInfo(MacroInfo* MI) {
43 MICache.push_back(MI);
44 MI->FreeArgumentList(BP);
45}
46
47
48/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
49/// current line until the tok::eom token is found.
50void Preprocessor::DiscardUntilEndOfDirective() {
51 Token Tmp;
52 do {
53 LexUnexpandedToken(Tmp);
54 } while (Tmp.isNot(tok::eom));
55}
56
57/// ReadMacroName - Lex and validate a macro name, which occurs after a
58/// #define or #undef. This sets the token kind to eom and discards the rest
59/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
60/// this is due to a a #define, 2 if #undef directive, 0 if it is something
61/// else (e.g. #ifdef).
62void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
63 // Read the token, don't allow macro expansion on it.
64 LexUnexpandedToken(MacroNameTok);
65
66 // Missing macro name?
67 if (MacroNameTok.is(tok::eom)) {
68 Diag(MacroNameTok, diag::err_pp_missing_macro_name);
69 return;
70 }
71
72 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
73 if (II == 0) {
74 std::string Spelling = getSpelling(MacroNameTok);
75 const IdentifierInfo &Info = Identifiers.get(Spelling);
76 if (Info.isCPlusPlusOperatorKeyword())
77 // C++ 2.5p2: Alternative tokens behave the same as its primary token
78 // except for their spellings.
79 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling;
80 else
81 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
82 // Fall through on error.
83 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
84 // Error if defining "defined": C99 6.10.8.4.
85 Diag(MacroNameTok, diag::err_defined_macro_name);
86 } else if (isDefineUndef && II->hasMacroDefinition() &&
87 getMacroInfo(II)->isBuiltinMacro()) {
88 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
89 if (isDefineUndef == 1)
90 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
91 else
92 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
93 } else {
94 // Okay, we got a good identifier node. Return it.
95 return;
96 }
97
98 // Invalid macro name, read and discard the rest of the line. Then set the
99 // token kind to tok::eom.
100 MacroNameTok.setKind(tok::eom);
101 return DiscardUntilEndOfDirective();
102}
103
104/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
105/// not, emit a diagnostic and consume up until the eom. If EnableMacros is
106/// true, then we consider macros that expand to zero tokens as being ok.
107void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
108 Token Tmp;
109 // Lex unexpanded tokens for most directives: macros might expand to zero
110 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
111 // #line) allow empty macros.
112 if (EnableMacros)
113 Lex(Tmp);
114 else
115 LexUnexpandedToken(Tmp);
116
117 // There should be no tokens after the directive, but we allow them as an
118 // extension.
119 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
120 LexUnexpandedToken(Tmp);
121
122 if (Tmp.isNot(tok::eom)) {
123 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
124 // because it is more trouble than it is worth to insert /**/ and check that
125 // there is no /**/ in the range also.
126 CodeModificationHint FixItHint;
127 if (Features.GNUMode || Features.C99 || Features.CPlusPlus)
128 FixItHint = CodeModificationHint::CreateInsertion(Tmp.getLocation(),"//");
129 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << FixItHint;
130 DiscardUntilEndOfDirective();
131 }
132}
133
134
135
136/// SkipExcludedConditionalBlock - We just read a #if or related directive and
137/// decided that the subsequent tokens are in the #if'd out portion of the
138/// file. Lex the rest of the file, until we see an #endif. If
139/// FoundNonSkipPortion is true, then we have already emitted code for part of
140/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
141/// is true, then #else directives are ok, if not, then we have already seen one
142/// so a #else directive is a duplicate. When this returns, the caller can lex
143/// the first valid token.
144void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
145 bool FoundNonSkipPortion,
146 bool FoundElse) {
147 ++NumSkipped;
148 assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?");
149
150 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
151 FoundNonSkipPortion, FoundElse);
152
153 if (CurPTHLexer) {
154 PTHSkipExcludedConditionalBlock();
155 return;
156 }
157
158 // Enter raw mode to disable identifier lookup (and thus macro expansion),
159 // disabling warnings, etc.
160 CurPPLexer->LexingRawMode = true;
161 Token Tok;
162 while (1) {
163 CurLexer->Lex(Tok);
164
165 // If this is the end of the buffer, we have an error.
166 if (Tok.is(tok::eof)) {
167 // Emit errors for each unterminated conditional on the stack, including
168 // the current one.
169 while (!CurPPLexer->ConditionalStack.empty()) {
170 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
171 diag::err_pp_unterminated_conditional);
172 CurPPLexer->ConditionalStack.pop_back();
173 }
174
175 // Just return and let the caller lex after this #include.
176 break;
177 }
178
179 // If this token is not a preprocessor directive, just skip it.
180 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
181 continue;
182
183 // We just parsed a # character at the start of a line, so we're in
184 // directive mode. Tell the lexer this so any newlines we see will be
185 // converted into an EOM token (this terminates the macro).
186 CurPPLexer->ParsingPreprocessorDirective = true;
187 if (CurLexer) CurLexer->SetCommentRetentionState(false);
188
189
190 // Read the next token, the directive flavor.
191 LexUnexpandedToken(Tok);
192
193 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
194 // something bogus), skip it.
195 if (Tok.isNot(tok::identifier)) {
196 CurPPLexer->ParsingPreprocessorDirective = false;
197 // Restore comment saving mode.
198 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
199 continue;
200 }
201
202 // If the first letter isn't i or e, it isn't intesting to us. We know that
203 // this is safe in the face of spelling differences, because there is no way
204 // to spell an i/e in a strange way that is another letter. Skipping this
205 // allows us to avoid looking up the identifier info for #define/#undef and
206 // other common directives.
207 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
208 char FirstChar = RawCharData[0];
209 if (FirstChar >= 'a' && FirstChar <= 'z' &&
210 FirstChar != 'i' && FirstChar != 'e') {
211 CurPPLexer->ParsingPreprocessorDirective = false;
212 // Restore comment saving mode.
213 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
214 continue;
215 }
216
217 // Get the identifier name without trigraphs or embedded newlines. Note
218 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
219 // when skipping.
220 char DirectiveBuf[20];
221 llvm::StringRef Directive;
222 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
223 Directive = llvm::StringRef(RawCharData, Tok.getLength());
224 } else {
225 std::string DirectiveStr = getSpelling(Tok);
226 unsigned IdLen = DirectiveStr.size();
227 if (IdLen >= 20) {
228 CurPPLexer->ParsingPreprocessorDirective = false;
229 // Restore comment saving mode.
230 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
231 continue;
232 }
233 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
234 Directive = llvm::StringRef(DirectiveBuf, IdLen);
235 }
236
237 if (Directive.startswith("if")) {
238 llvm::StringRef Sub = Directive.substr(2);
239 if (Sub.empty() || // "if"
240 Sub == "def" || // "ifdef"
241 Sub == "ndef") { // "ifndef"
242 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
243 // bother parsing the condition.
244 DiscardUntilEndOfDirective();
245 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
246 /*foundnonskip*/false,
247 /*fnddelse*/false);
248 }
249 } else if (Directive[0] == 'e') {
250 llvm::StringRef Sub = Directive.substr(1);
251 if (Sub == "ndif") { // "endif"
252 CheckEndOfDirective("endif");
253 PPConditionalInfo CondInfo;
254 CondInfo.WasSkipping = true; // Silence bogus warning.
255 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
256 InCond = InCond; // Silence warning in no-asserts mode.
257 assert(!InCond && "Can't be skipping if not in a conditional!");
258
259 // If we popped the outermost skipping block, we're done skipping!
260 if (!CondInfo.WasSkipping)
261 break;
262 } else if (Sub == "lse") { // "else".
263 // #else directive in a skipping conditional. If not in some other
264 // skipping conditional, and if #else hasn't already been seen, enter it
265 // as a non-skipping conditional.
266 DiscardUntilEndOfDirective(); // C99 6.10p4.
267 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
268
269 // If this is a #else with a #else before it, report the error.
270 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
271
272 // Note that we've seen a #else in this conditional.
273 CondInfo.FoundElse = true;
274
275 // If the conditional is at the top level, and the #if block wasn't
276 // entered, enter the #else block now.
277 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
278 CondInfo.FoundNonSkip = true;
279 break;
280 }
281 } else if (Sub == "lif") { // "elif".
282 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
283
284 bool ShouldEnter;
285 // If this is in a skipping block or if we're already handled this #if
286 // block, don't bother parsing the condition.
287 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
288 DiscardUntilEndOfDirective();
289 ShouldEnter = false;
290 } else {
291 // Restore the value of LexingRawMode so that identifiers are
292 // looked up, etc, inside the #elif expression.
293 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
294 CurPPLexer->LexingRawMode = false;
295 IdentifierInfo *IfNDefMacro = 0;
296 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
297 CurPPLexer->LexingRawMode = true;
298 }
299
300 // If this is a #elif with a #else before it, report the error.
301 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
302
303 // If this condition is true, enter it!
304 if (ShouldEnter) {
305 CondInfo.FoundNonSkip = true;
306 break;
307 }
308 }
309 }
310
311 CurPPLexer->ParsingPreprocessorDirective = false;
312 // Restore comment saving mode.
313 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
314 }
315
316 // Finally, if we are out of the conditional (saw an #endif or ran off the end
317 // of the file, just stop skipping and return to lexing whatever came after
318 // the #if block.
319 CurPPLexer->LexingRawMode = false;
320}
321
322void Preprocessor::PTHSkipExcludedConditionalBlock() {
323
324 while (1) {
325 assert(CurPTHLexer);
326 assert(CurPTHLexer->LexingRawMode == false);
327
328 // Skip to the next '#else', '#elif', or #endif.
329 if (CurPTHLexer->SkipBlock()) {
330 // We have reached an #endif. Both the '#' and 'endif' tokens
331 // have been consumed by the PTHLexer. Just pop off the condition level.
332 PPConditionalInfo CondInfo;
333 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
334 InCond = InCond; // Silence warning in no-asserts mode.
335 assert(!InCond && "Can't be skipping if not in a conditional!");
336 break;
337 }
338
339 // We have reached a '#else' or '#elif'. Lex the next token to get
340 // the directive flavor.
341 Token Tok;
342 LexUnexpandedToken(Tok);
343
344 // We can actually look up the IdentifierInfo here since we aren't in
345 // raw mode.
346 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
347
348 if (K == tok::pp_else) {
349 // #else: Enter the else condition. We aren't in a nested condition
350 // since we skip those. We're always in the one matching the last
351 // blocked we skipped.
352 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
353 // Note that we've seen a #else in this conditional.
354 CondInfo.FoundElse = true;
355
356 // If the #if block wasn't entered then enter the #else block now.
357 if (!CondInfo.FoundNonSkip) {
358 CondInfo.FoundNonSkip = true;
359
360 // Scan until the eom token.
361 CurPTHLexer->ParsingPreprocessorDirective = true;
362 DiscardUntilEndOfDirective();
363 CurPTHLexer->ParsingPreprocessorDirective = false;
364
365 break;
366 }
367
368 // Otherwise skip this block.
369 continue;
370 }
371
372 assert(K == tok::pp_elif);
373 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
374
375 // If this is a #elif with a #else before it, report the error.
376 if (CondInfo.FoundElse)
377 Diag(Tok, diag::pp_err_elif_after_else);
378
379 // If this is in a skipping block or if we're already handled this #if
380 // block, don't bother parsing the condition. We just skip this block.
381 if (CondInfo.FoundNonSkip)
382 continue;
383
384 // Evaluate the condition of the #elif.
385 IdentifierInfo *IfNDefMacro = 0;
386 CurPTHLexer->ParsingPreprocessorDirective = true;
387 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
388 CurPTHLexer->ParsingPreprocessorDirective = false;
389
390 // If this condition is true, enter it!
391 if (ShouldEnter) {
392 CondInfo.FoundNonSkip = true;
393 break;
394 }
395
396 // Otherwise, skip this block and go to the next one.
397 continue;
398 }
399}
400
401/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
402/// return null on failure. isAngled indicates whether the file reference is
403/// for system #include's or not (i.e. using <> instead of "").
404const FileEntry *Preprocessor::LookupFile(llvm::StringRef Filename,
405 bool isAngled,
406 const DirectoryLookup *FromDir,
407 const DirectoryLookup *&CurDir) {
408 // If the header lookup mechanism may be relative to the current file, pass in
409 // info about where the current file is.
410 const FileEntry *CurFileEnt = 0;
411 if (!FromDir) {
412 FileID FID = getCurrentFileLexer()->getFileID();
413 CurFileEnt = SourceMgr.getFileEntryForID(FID);
414
415 // If there is no file entry associated with this file, it must be the
416 // predefines buffer. Any other file is not lexed with a normal lexer, so
417 // it won't be scanned for preprocessor directives. If we have the
418 // predefines buffer, resolve #include references (which come from the
419 // -include command line argument) as if they came from the main file, this
420 // affects file lookup etc.
421 if (CurFileEnt == 0) {
422 FID = SourceMgr.getMainFileID();
423 CurFileEnt = SourceMgr.getFileEntryForID(FID);
424 }
425 }
426
427 // Do a standard file entry lookup.
428 CurDir = CurDirLookup;
429 const FileEntry *FE =
430 HeaderInfo.LookupFile(Filename, isAngled, FromDir, CurDir, CurFileEnt);
431 if (FE) return FE;
432
433 // Otherwise, see if this is a subframework header. If so, this is relative
434 // to one of the headers on the #include stack. Walk the list of the current
435 // headers on the #include stack and pass them to HeaderInfo.
436 if (IsFileLexer()) {
437 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
438 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
439 return FE;
440 }
441
442 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
443 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
444 if (IsFileLexer(ISEntry)) {
445 if ((CurFileEnt =
446 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
447 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
448 return FE;
449 }
450 }
451
452 // Otherwise, we really couldn't find the file.
453 return 0;
454}
455
456
457//===----------------------------------------------------------------------===//
458// Preprocessor Directive Handling.
459//===----------------------------------------------------------------------===//
460
461/// HandleDirective - This callback is invoked when the lexer sees a # token
462/// at the start of a line. This consumes the directive, modifies the
463/// lexer/preprocessor state, and advances the lexer(s) so that the next token
464/// read is the correct one.
465void Preprocessor::HandleDirective(Token &Result) {
466 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
467
468 // We just parsed a # character at the start of a line, so we're in directive
469 // mode. Tell the lexer this so any newlines we see will be converted into an
470 // EOM token (which terminates the directive).
471 CurPPLexer->ParsingPreprocessorDirective = true;
472
473 ++NumDirectives;
474
475 // We are about to read a token. For the multiple-include optimization FA to
476 // work, we have to remember if we had read any tokens *before* this
477 // pp-directive.
478 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
479
480 // Save the '#' token in case we need to return it later.
481 Token SavedHash = Result;
482
483 // Read the next token, the directive flavor. This isn't expanded due to
484 // C99 6.10.3p8.
485 LexUnexpandedToken(Result);
486
487 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
488 // #define A(x) #x
489 // A(abc
490 // #warning blah
491 // def)
492 // If so, the user is relying on non-portable behavior, emit a diagnostic.
493 if (InMacroArgs)
494 Diag(Result, diag::ext_embedded_directive);
495
496TryAgain:
497 switch (Result.getKind()) {
498 case tok::eom:
499 return; // null directive.
500 case tok::comment:
501 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
502 LexUnexpandedToken(Result);
503 goto TryAgain;
504
505 case tok::numeric_constant: // # 7 GNU line marker directive.
506 if (getLangOptions().AsmPreprocessor)
507 break; // # 4 is not a preprocessor directive in .S files.
508 return HandleDigitDirective(Result);
509 default:
510 IdentifierInfo *II = Result.getIdentifierInfo();
511 if (II == 0) break; // Not an identifier.
512
513 // Ask what the preprocessor keyword ID is.
514 switch (II->getPPKeywordID()) {
515 default: break;
516 // C99 6.10.1 - Conditional Inclusion.
517 case tok::pp_if:
518 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
519 case tok::pp_ifdef:
520 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
521 case tok::pp_ifndef:
522 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
523 case tok::pp_elif:
524 return HandleElifDirective(Result);
525 case tok::pp_else:
526 return HandleElseDirective(Result);
527 case tok::pp_endif:
528 return HandleEndifDirective(Result);
529
530 // C99 6.10.2 - Source File Inclusion.
531 case tok::pp_include:
532 return HandleIncludeDirective(Result); // Handle #include.
533 case tok::pp___include_macros:
534 return HandleIncludeMacrosDirective(Result); // Handle -imacros.
535
536 // C99 6.10.3 - Macro Replacement.
537 case tok::pp_define:
538 return HandleDefineDirective(Result);
539 case tok::pp_undef:
540 return HandleUndefDirective(Result);
541
542 // C99 6.10.4 - Line Control.
543 case tok::pp_line:
544 return HandleLineDirective(Result);
545
546 // C99 6.10.5 - Error Directive.
547 case tok::pp_error:
548 return HandleUserDiagnosticDirective(Result, false);
549
550 // C99 6.10.6 - Pragma Directive.
551 case tok::pp_pragma:
552 return HandlePragmaDirective();
553
554 // GNU Extensions.
555 case tok::pp_import:
556 return HandleImportDirective(Result);
557 case tok::pp_include_next:
558 return HandleIncludeNextDirective(Result);
559
560 case tok::pp_warning:
561 Diag(Result, diag::ext_pp_warning_directive);
562 return HandleUserDiagnosticDirective(Result, true);
563 case tok::pp_ident:
564 return HandleIdentSCCSDirective(Result);
565 case tok::pp_sccs:
566 return HandleIdentSCCSDirective(Result);
567 case tok::pp_assert:
568 //isExtension = true; // FIXME: implement #assert
569 break;
570 case tok::pp_unassert:
571 //isExtension = true; // FIXME: implement #unassert
572 break;
573 }
574 break;
575 }
576
577 // If this is a .S file, treat unknown # directives as non-preprocessor
578 // directives. This is important because # may be a comment or introduce
579 // various pseudo-ops. Just return the # token and push back the following
580 // token to be lexed next time.
581 if (getLangOptions().AsmPreprocessor) {
582 Token *Toks = new Token[2];
583 // Return the # and the token after it.
584 Toks[0] = SavedHash;
585 Toks[1] = Result;
586 // Enter this token stream so that we re-lex the tokens. Make sure to
587 // enable macro expansion, in case the token after the # is an identifier
588 // that is expanded.
589 EnterTokenStream(Toks, 2, false, true);
590 return;
591 }
592
593 // If we reached here, the preprocessing token is not valid!
594 Diag(Result, diag::err_pp_invalid_directive);
595
596 // Read the rest of the PP line.
597 DiscardUntilEndOfDirective();
598
599 // Okay, we're done parsing the directive.
600}
601
602/// GetLineValue - Convert a numeric token into an unsigned value, emitting
603/// Diagnostic DiagID if it is invalid, and returning the value in Val.
604static bool GetLineValue(Token &DigitTok, unsigned &Val,
605 unsigned DiagID, Preprocessor &PP) {
606 if (DigitTok.isNot(tok::numeric_constant)) {
607 PP.Diag(DigitTok, DiagID);
608
609 if (DigitTok.isNot(tok::eom))
610 PP.DiscardUntilEndOfDirective();
611 return true;
612 }
613
614 llvm::SmallString<64> IntegerBuffer;
615 IntegerBuffer.resize(DigitTok.getLength());
616 const char *DigitTokBegin = &IntegerBuffer[0];
617 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin);
618
619 // Verify that we have a simple digit-sequence, and compute the value. This
620 // is always a simple digit string computed in decimal, so we do this manually
621 // here.
622 Val = 0;
623 for (unsigned i = 0; i != ActualLength; ++i) {
624 if (!isdigit(DigitTokBegin[i])) {
625 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
626 diag::err_pp_line_digit_sequence);
627 PP.DiscardUntilEndOfDirective();
628 return true;
629 }
630
631 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
632 if (NextVal < Val) { // overflow.
633 PP.Diag(DigitTok, DiagID);
634 PP.DiscardUntilEndOfDirective();
635 return true;
636 }
637 Val = NextVal;
638 }
639
640 // Reject 0, this is needed both by #line numbers and flags.
641 if (Val == 0) {
642 PP.Diag(DigitTok, DiagID);
643 PP.DiscardUntilEndOfDirective();
644 return true;
645 }
646
647 if (DigitTokBegin[0] == '0')
648 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal);
649
650 return false;
651}
652
653/// HandleLineDirective - Handle #line directive: C99 6.10.4. The two
654/// acceptable forms are:
655/// # line digit-sequence
656/// # line digit-sequence "s-char-sequence"
657void Preprocessor::HandleLineDirective(Token &Tok) {
658 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
659 // expanded.
660 Token DigitTok;
661 Lex(DigitTok);
662
663 // Validate the number and convert it to an unsigned.
664 unsigned LineNo;
665 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
666 return;
667
668 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
669 // number greater than 2147483647". C90 requires that the line # be <= 32767.
670 unsigned LineLimit = Features.C99 ? 2147483648U : 32768U;
671 if (LineNo >= LineLimit)
672 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
673
674 int FilenameID = -1;
675 Token StrTok;
676 Lex(StrTok);
677
678 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a
679 // string followed by eom.
680 if (StrTok.is(tok::eom))
681 ; // ok
682 else if (StrTok.isNot(tok::string_literal)) {
683 Diag(StrTok, diag::err_pp_line_invalid_filename);
684 DiscardUntilEndOfDirective();
685 return;
686 } else {
687 // Parse and validate the string, converting it into a unique ID.
688 StringLiteralParser Literal(&StrTok, 1, *this);
689 assert(!Literal.AnyWide && "Didn't allow wide strings in");
690 if (Literal.hadError)
691 return DiscardUntilEndOfDirective();
692 if (Literal.Pascal) {
693 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
694 return DiscardUntilEndOfDirective();
695 }
696 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(),
697 Literal.GetStringLength());
698
699 // Verify that there is nothing after the string, other than EOM. Because
700 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
701 CheckEndOfDirective("line", true);
702 }
703
704 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
705
706 if (Callbacks)
707 Callbacks->FileChanged(DigitTok.getLocation(), PPCallbacks::RenameFile,
708 SrcMgr::C_User);
709}
710
711/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
712/// marker directive.
713static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
714 bool &IsSystemHeader, bool &IsExternCHeader,
715 Preprocessor &PP) {
716 unsigned FlagVal;
717 Token FlagTok;
718 PP.Lex(FlagTok);
719 if (FlagTok.is(tok::eom)) return false;
720 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
721 return true;
722
723 if (FlagVal == 1) {
724 IsFileEntry = true;
725
726 PP.Lex(FlagTok);
727 if (FlagTok.is(tok::eom)) return false;
728 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
729 return true;
730 } else if (FlagVal == 2) {
731 IsFileExit = true;
732
733 SourceManager &SM = PP.getSourceManager();
734 // If we are leaving the current presumed file, check to make sure the
735 // presumed include stack isn't empty!
736 FileID CurFileID =
737 SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first;
738 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
739
740 // If there is no include loc (main file) or if the include loc is in a
741 // different physical file, then we aren't in a "1" line marker flag region.
742 SourceLocation IncLoc = PLoc.getIncludeLoc();
743 if (IncLoc.isInvalid() ||
744 SM.getDecomposedInstantiationLoc(IncLoc).first != CurFileID) {
745 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
746 PP.DiscardUntilEndOfDirective();
747 return true;
748 }
749
750 PP.Lex(FlagTok);
751 if (FlagTok.is(tok::eom)) return false;
752 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
753 return true;
754 }
755
756 // We must have 3 if there are still flags.
757 if (FlagVal != 3) {
758 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
759 PP.DiscardUntilEndOfDirective();
760 return true;
761 }
762
763 IsSystemHeader = true;
764
765 PP.Lex(FlagTok);
766 if (FlagTok.is(tok::eom)) return false;
767 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
768 return true;
769
770 // We must have 4 if there is yet another flag.
771 if (FlagVal != 4) {
772 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
773 PP.DiscardUntilEndOfDirective();
774 return true;
775 }
776
777 IsExternCHeader = true;
778
779 PP.Lex(FlagTok);
780 if (FlagTok.is(tok::eom)) return false;
781
782 // There are no more valid flags here.
783 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
784 PP.DiscardUntilEndOfDirective();
785 return true;
786}
787
788/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
789/// one of the following forms:
790///
791/// # 42
792/// # 42 "file" ('1' | '2')?
793/// # 42 "file" ('1' | '2')? '3' '4'?
794///
795void Preprocessor::HandleDigitDirective(Token &DigitTok) {
796 // Validate the number and convert it to an unsigned. GNU does not have a
797 // line # limit other than it fit in 32-bits.
798 unsigned LineNo;
799 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
800 *this))
801 return;
802
803 Token StrTok;
804 Lex(StrTok);
805
806 bool IsFileEntry = false, IsFileExit = false;
807 bool IsSystemHeader = false, IsExternCHeader = false;
808 int FilenameID = -1;
809
810 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a
811 // string followed by eom.
812 if (StrTok.is(tok::eom))
813 ; // ok
814 else if (StrTok.isNot(tok::string_literal)) {
815 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
816 return DiscardUntilEndOfDirective();
817 } else {
818 // Parse and validate the string, converting it into a unique ID.
819 StringLiteralParser Literal(&StrTok, 1, *this);
820 assert(!Literal.AnyWide && "Didn't allow wide strings in");
821 if (Literal.hadError)
822 return DiscardUntilEndOfDirective();
823 if (Literal.Pascal) {
824 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
825 return DiscardUntilEndOfDirective();
826 }
827 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(),
828 Literal.GetStringLength());
829
830 // If a filename was present, read any flags that are present.
831 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
832 IsSystemHeader, IsExternCHeader, *this))
833 return;
834 }
835
836 // Create a line note with this information.
837 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
838 IsFileEntry, IsFileExit,
839 IsSystemHeader, IsExternCHeader);
840
841 // If the preprocessor has callbacks installed, notify them of the #line
842 // change. This is used so that the line marker comes out in -E mode for
843 // example.
844 if (Callbacks) {
845 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
846 if (IsFileEntry)
847 Reason = PPCallbacks::EnterFile;
848 else if (IsFileExit)
849 Reason = PPCallbacks::ExitFile;
850 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
851 if (IsExternCHeader)
852 FileKind = SrcMgr::C_ExternCSystem;
853 else if (IsSystemHeader)
854 FileKind = SrcMgr::C_System;
855
856 Callbacks->FileChanged(DigitTok.getLocation(), Reason, FileKind);
857 }
858}
859
860
861/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
862///
863void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
864 bool isWarning) {
865 // PTH doesn't emit #warning or #error directives.
866 if (CurPTHLexer)
867 return CurPTHLexer->DiscardToEndOfLine();
868
869 // Read the rest of the line raw. We do this because we don't want macros
870 // to be expanded and we don't require that the tokens be valid preprocessing
871 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
872 // collapse multiple consequtive white space between tokens, but this isn't
873 // specified by the standard.
874 std::string Message = CurLexer->ReadToEndOfLine();
875 if (isWarning)
876 Diag(Tok, diag::pp_hash_warning) << Message;
877 else
878 Diag(Tok, diag::err_pp_hash_error) << Message;
879}
880
881/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
882///
883void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
884 // Yes, this directive is an extension.
885 Diag(Tok, diag::ext_pp_ident_directive);
886
887 // Read the string argument.
888 Token StrTok;
889 Lex(StrTok);
890
891 // If the token kind isn't a string, it's a malformed directive.
892 if (StrTok.isNot(tok::string_literal) &&
893 StrTok.isNot(tok::wide_string_literal)) {
894 Diag(StrTok, diag::err_pp_malformed_ident);
895 if (StrTok.isNot(tok::eom))
896 DiscardUntilEndOfDirective();
897 return;
898 }
899
900 // Verify that there is nothing after the string, other than EOM.
901 CheckEndOfDirective("ident");
902
903 if (Callbacks)
904 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
905}
906
907//===----------------------------------------------------------------------===//
908// Preprocessor Include Directive Handling.
909//===----------------------------------------------------------------------===//
910
911/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
912/// checked and spelled filename, e.g. as an operand of #include. This returns
913/// true if the input filename was in <>'s or false if it were in ""'s. The
914/// caller is expected to provide a buffer that is large enough to hold the
915/// spelling of the filename, but is also expected to handle the case when
916/// this method decides to use a different buffer.
917bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
918 llvm::StringRef &Buffer) {
919 // Get the text form of the filename.
920 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
921
922 // Make sure the filename is <x> or "x".
923 bool isAngled;
924 if (Buffer[0] == '<') {
925 if (Buffer.back() != '>') {
926 Diag(Loc, diag::err_pp_expects_filename);
927 Buffer = llvm::StringRef();
928 return true;
929 }
930 isAngled = true;
931 } else if (Buffer[0] == '"') {
932 if (Buffer.back() != '"') {
933 Diag(Loc, diag::err_pp_expects_filename);
934 Buffer = llvm::StringRef();
935 return true;
936 }
937 isAngled = false;
938 } else {
939 Diag(Loc, diag::err_pp_expects_filename);
940 Buffer = llvm::StringRef();
941 return true;
942 }
943
944 // Diagnose #include "" as invalid.
945 if (Buffer.size() <= 2) {
946 Diag(Loc, diag::err_pp_empty_filename);
947 Buffer = llvm::StringRef();
948 return true;
949 }
950
951 // Skip the brackets.
952 Buffer = Buffer.substr(1, Buffer.size()-2);
953 return isAngled;
954}
955
956/// ConcatenateIncludeName - Handle cases where the #include name is expanded
957/// from a macro as multiple tokens, which need to be glued together. This
958/// occurs for code like:
959/// #define FOO <a/b.h>
960/// #include FOO
961/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
962///
963/// This code concatenates and consumes tokens up to the '>' token. It returns
964/// false if the > was found, otherwise it returns true if it finds and consumes
965/// the EOM marker.
966bool Preprocessor::ConcatenateIncludeName(
967 llvm::SmallVector<char, 128> &FilenameBuffer) {
968 Token CurTok;
969
970 Lex(CurTok);
971 while (CurTok.isNot(tok::eom)) {
972 // Append the spelling of this token to the buffer. If there was a space
973 // before it, add it now.
974 if (CurTok.hasLeadingSpace())
975 FilenameBuffer.push_back(' ');
976
977 // Get the spelling of the token, directly into FilenameBuffer if possible.
978 unsigned PreAppendSize = FilenameBuffer.size();
979 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
980
981 const char *BufPtr = &FilenameBuffer[PreAppendSize];
982 unsigned ActualLen = getSpelling(CurTok, BufPtr);
983
984 // If the token was spelled somewhere else, copy it into FilenameBuffer.
985 if (BufPtr != &FilenameBuffer[PreAppendSize])
986 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
987
988 // Resize FilenameBuffer to the correct size.
989 if (CurTok.getLength() != ActualLen)
990 FilenameBuffer.resize(PreAppendSize+ActualLen);
991
992 // If we found the '>' marker, return success.
993 if (CurTok.is(tok::greater))
994 return false;
995
996 Lex(CurTok);
997 }
998
999 // If we hit the eom marker, emit an error and return true so that the caller
1000 // knows the EOM has been read.
1001 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1002 return true;
1003}
1004
1005/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1006/// file to be included from the lexer, then include it! This is a common
1007/// routine with functionality shared between #include, #include_next and
1008/// #import. LookupFrom is set when this is a #include_next directive, it
1009/// specifies the file to start searching from.
1010void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
1011 const DirectoryLookup *LookupFrom,
1012 bool isImport) {
1013
1014 Token FilenameTok;
1015 CurPPLexer->LexIncludeFilename(FilenameTok);
1016
1017 // Reserve a buffer to get the spelling.
1018 llvm::SmallString<128> FilenameBuffer;
1019 llvm::StringRef Filename;
1020
1021 switch (FilenameTok.getKind()) {
1022 case tok::eom:
1023 // If the token kind is EOM, the error has already been diagnosed.
1024 return;
1025
1026 case tok::angle_string_literal:
1027 case tok::string_literal: {
1028 FilenameBuffer.resize(FilenameTok.getLength());
1029 const char *FilenameStart = &FilenameBuffer[0];
1030 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1031 Filename = llvm::StringRef(FilenameStart, Len);
1032 break;
1033 }
1034
1035 case tok::less:
1036 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1037 // case, glue the tokens together into FilenameBuffer and interpret those.
1038 FilenameBuffer.push_back('<');
1039 if (ConcatenateIncludeName(FilenameBuffer))
1040 return; // Found <eom> but no ">"? Diagnostic already emitted.
1041 Filename = FilenameBuffer.str();
1042 break;
1043 default:
1044 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1045 DiscardUntilEndOfDirective();
1046 return;
1047 }
1048
1049 bool isAngled =
1050 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1051 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1052 // error.
1053 if (Filename.empty()) {
1054 DiscardUntilEndOfDirective();
1055 return;
1056 }
1057
1058 // Verify that there is nothing after the filename, other than EOM. Note that
1059 // we allow macros that expand to nothing after the filename, because this
1060 // falls into the category of "#include pp-tokens new-line" specified in
1061 // C99 6.10.2p4.
1062 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1063
1064 // Check that we don't have infinite #include recursion.
1065 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1066 Diag(FilenameTok, diag::err_pp_include_too_deep);
1067 return;
1068 }
1069
1070 // Search include directories.
1071 const DirectoryLookup *CurDir;
1072 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
1073 if (File == 0) {
1074 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1075 return;
1076 }
1077
1078 // Ask HeaderInfo if we should enter this #include file. If not, #including
1079 // this file will have no effect.
1080 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport))
1081 return;
1082
1083 // The #included file will be considered to be a system header if either it is
1084 // in a system include directory, or if the #includer is a system include
1085 // header.
1086 SrcMgr::CharacteristicKind FileCharacter =
1087 std::max(HeaderInfo.getFileDirFlavor(File),
1088 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1089
1090 // Look up the file, create a File ID for it.
1091 FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(),
1092 FileCharacter);
1093 if (FID.isInvalid()) {
1094 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1095 return;
1096 }
1097
1098 // Finally, if all is good, enter the new file!
1099 std::string ErrorStr;
1100 if (EnterSourceFile(FID, CurDir, ErrorStr))
1101 Diag(FilenameTok, diag::err_pp_error_opening_file)
1102 << std::string(SourceMgr.getFileEntryForID(FID)->getName()) << ErrorStr;
1103}
1104
1105/// HandleIncludeNextDirective - Implements #include_next.
1106///
1107void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
1108 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1109
1110 // #include_next is like #include, except that we start searching after
1111 // the current found directory. If we can't do this, issue a
1112 // diagnostic.
1113 const DirectoryLookup *Lookup = CurDirLookup;
1114 if (isInPrimaryFile()) {
1115 Lookup = 0;
1116 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1117 } else if (Lookup == 0) {
1118 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1119 } else {
1120 // Start looking up in the next directory.
1121 ++Lookup;
1122 }
1123
1124 return HandleIncludeDirective(IncludeNextTok, Lookup);
1125}
1126
1127/// HandleImportDirective - Implements #import.
1128///
1129void Preprocessor::HandleImportDirective(Token &ImportTok) {
1130 if (!Features.ObjC1) // #import is standard for ObjC.
1131 Diag(ImportTok, diag::ext_pp_import_directive);
1132
1133 return HandleIncludeDirective(ImportTok, 0, true);
1134}
1135
1136/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1137/// pseudo directive in the predefines buffer. This handles it by sucking all
1138/// tokens through the preprocessor and discarding them (only keeping the side
1139/// effects on the preprocessor).
1140void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) {
1141 // This directive should only occur in the predefines buffer. If not, emit an
1142 // error and reject it.
1143 SourceLocation Loc = IncludeMacrosTok.getLocation();
1144 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1145 Diag(IncludeMacrosTok.getLocation(),
1146 diag::pp_include_macros_out_of_predefines);
1147 DiscardUntilEndOfDirective();
1148 return;
1149 }
1150
1151 // Treat this as a normal #include for checking purposes. If this is
1152 // successful, it will push a new lexer onto the include stack.
1153 HandleIncludeDirective(IncludeMacrosTok, 0, false);
1154
1155 Token TmpTok;
1156 do {
1157 Lex(TmpTok);
1158 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1159 } while (TmpTok.isNot(tok::hashhash));
1160}
1161
1162//===----------------------------------------------------------------------===//
1163// Preprocessor Macro Directive Handling.
1164//===----------------------------------------------------------------------===//
1165
1166/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1167/// definition has just been read. Lex the rest of the arguments and the
1168/// closing ), updating MI with what we learn. Return true if an error occurs
1169/// parsing the arg list.
1170bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
1171 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
1172
1173 Token Tok;
1174 while (1) {
1175 LexUnexpandedToken(Tok);
1176 switch (Tok.getKind()) {
1177 case tok::r_paren:
1178 // Found the end of the argument list.
1179 if (Arguments.empty()) // #define FOO()
1180 return false;
1181 // Otherwise we have #define FOO(A,)
1182 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1183 return true;
1184 case tok::ellipsis: // #define X(... -> C99 varargs
1185 // Warn if use of C99 feature in non-C99 mode.
1186 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1187
1188 // Lex the token after the identifier.
1189 LexUnexpandedToken(Tok);
1190 if (Tok.isNot(tok::r_paren)) {
1191 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1192 return true;
1193 }
1194 // Add the __VA_ARGS__ identifier as an argument.
1195 Arguments.push_back(Ident__VA_ARGS__);
1196 MI->setIsC99Varargs();
1197 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1198 return false;
1199 case tok::eom: // #define X(
1200 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1201 return true;
1202 default:
1203 // Handle keywords and identifiers here to accept things like
1204 // #define Foo(for) for.
1205 IdentifierInfo *II = Tok.getIdentifierInfo();
1206 if (II == 0) {
1207 // #define X(1
1208 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1209 return true;
1210 }
1211
1212 // If this is already used as an argument, it is used multiple times (e.g.
1213 // #define X(A,A.
1214 if (std::find(Arguments.begin(), Arguments.end(), II) !=
1215 Arguments.end()) { // C99 6.10.3p6
1216 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1217 return true;
1218 }
1219
1220 // Add the argument to the macro info.
1221 Arguments.push_back(II);
1222
1223 // Lex the token after the identifier.
1224 LexUnexpandedToken(Tok);
1225
1226 switch (Tok.getKind()) {
1227 default: // #define X(A B
1228 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1229 return true;
1230 case tok::r_paren: // #define X(A)
1231 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1232 return false;
1233 case tok::comma: // #define X(A,
1234 break;
1235 case tok::ellipsis: // #define X(A... -> GCC extension
1236 // Diagnose extension.
1237 Diag(Tok, diag::ext_named_variadic_macro);
1238
1239 // Lex the token after the identifier.
1240 LexUnexpandedToken(Tok);
1241 if (Tok.isNot(tok::r_paren)) {
1242 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1243 return true;
1244 }
1245
1246 MI->setIsGNUVarargs();
1247 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1248 return false;
1249 }
1250 }
1251 }
1252}
1253
1254/// HandleDefineDirective - Implements #define. This consumes the entire macro
1255/// line then lets the caller lex the next real token.
1256void Preprocessor::HandleDefineDirective(Token &DefineTok) {
1257 ++NumDefined;
1258
1259 Token MacroNameTok;
1260 ReadMacroName(MacroNameTok, 1);
1261
1262 // Error reading macro name? If so, diagnostic already issued.
1263 if (MacroNameTok.is(tok::eom))
1264 return;
1265
1266 Token LastTok = MacroNameTok;
1267
1268 // If we are supposed to keep comments in #defines, reenable comment saving
1269 // mode.
1270 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
1271
1272 // Create the new macro.
1273 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
1274
1275 Token Tok;
1276 LexUnexpandedToken(Tok);
1277
1278 // If this is a function-like macro definition, parse the argument list,
1279 // marking each of the identifiers as being used as macro arguments. Also,
1280 // check other constraints on the first token of the macro body.
1281 if (Tok.is(tok::eom)) {
1282 // If there is no body to this macro, we have no special handling here.
1283 } else if (Tok.hasLeadingSpace()) {
1284 // This is a normal token with leading space. Clear the leading space
1285 // marker on the first token to get proper expansion.
1286 Tok.clearFlag(Token::LeadingSpace);
1287 } else if (Tok.is(tok::l_paren)) {
1288 // This is a function-like macro definition. Read the argument list.
1289 MI->setIsFunctionLike();
1290 if (ReadMacroDefinitionArgList(MI)) {
1291 // Forget about MI.
1292 ReleaseMacroInfo(MI);
1293 // Throw away the rest of the line.
1294 if (CurPPLexer->ParsingPreprocessorDirective)
1295 DiscardUntilEndOfDirective();
1296 return;
1297 }
1298
1299 // If this is a definition of a variadic C99 function-like macro, not using
1300 // the GNU named varargs extension, enabled __VA_ARGS__.
1301
1302 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1303 // This gets unpoisoned where it is allowed.
1304 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1305 if (MI->isC99Varargs())
1306 Ident__VA_ARGS__->setIsPoisoned(false);
1307
1308 // Read the first token after the arg list for down below.
1309 LexUnexpandedToken(Tok);
1310 } else if (Features.C99) {
1311 // C99 requires whitespace between the macro definition and the body. Emit
1312 // a diagnostic for something like "#define X+".
1313 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
1314 } else {
1315 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1316 // first character of a replacement list is not a character required by
1317 // subclause 5.2.1, then there shall be white-space separation between the
1318 // identifier and the replacement list.". 5.2.1 lists this set:
1319 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1320 // is irrelevant here.
1321 bool isInvalid = false;
1322 if (Tok.is(tok::at)) // @ is not in the list above.
1323 isInvalid = true;
1324 else if (Tok.is(tok::unknown)) {
1325 // If we have an unknown token, it is something strange like "`". Since
1326 // all of valid characters would have lexed into a single character
1327 // token of some sort, we know this is not a valid case.
1328 isInvalid = true;
1329 }
1330 if (isInvalid)
1331 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1332 else
1333 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
1334 }
1335
1336 if (!Tok.is(tok::eom))
1337 LastTok = Tok;
1338
1339 // Read the rest of the macro body.
1340 if (MI->isObjectLike()) {
1341 // Object-like macros are very simple, just read their body.
1342 while (Tok.isNot(tok::eom)) {
1343 LastTok = Tok;
1344 MI->AddTokenToBody(Tok);
1345 // Get the next token of the macro.
1346 LexUnexpandedToken(Tok);
1347 }
1348
1349 } else {
1350 // Otherwise, read the body of a function-like macro. While we are at it,
1351 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1352 // parameters in function-like macro expansions.
1353 while (Tok.isNot(tok::eom)) {
1354 LastTok = Tok;
1355
1356 if (Tok.isNot(tok::hash)) {
1357 MI->AddTokenToBody(Tok);
1358
1359 // Get the next token of the macro.
1360 LexUnexpandedToken(Tok);
1361 continue;
1362 }
1363
1364 // Get the next token of the macro.
1365 LexUnexpandedToken(Tok);
1366
1367 // Check for a valid macro arg identifier.
1368 if (Tok.getIdentifierInfo() == 0 ||
1369 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1370
1371 // If this is assembler-with-cpp mode, we accept random gibberish after
1372 // the '#' because '#' is often a comment character. However, change
1373 // the kind of the token to tok::unknown so that the preprocessor isn't
1374 // confused.
1375 if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eom)) {
1376 LastTok.setKind(tok::unknown);
1377 } else {
1378 Diag(Tok, diag::err_pp_stringize_not_parameter);
1379 ReleaseMacroInfo(MI);
1380
1381 // Disable __VA_ARGS__ again.
1382 Ident__VA_ARGS__->setIsPoisoned(true);
1383 return;
1384 }
1385 }
1386
1387 // Things look ok, add the '#' and param name tokens to the macro.
1388 MI->AddTokenToBody(LastTok);
1389 MI->AddTokenToBody(Tok);
1390 LastTok = Tok;
1391
1392 // Get the next token of the macro.
1393 LexUnexpandedToken(Tok);
1394 }
1395 }
1396
1397
1398 // Disable __VA_ARGS__ again.
1399 Ident__VA_ARGS__->setIsPoisoned(true);
1400
1401 // Check that there is no paste (##) operator at the begining or end of the
1402 // replacement list.
1403 unsigned NumTokens = MI->getNumTokens();
1404 if (NumTokens != 0) {
1405 if (MI->getReplacementToken(0).is(tok::hashhash)) {
1406 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
1407 ReleaseMacroInfo(MI);
1408 return;
1409 }
1410 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
1411 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
1412 ReleaseMacroInfo(MI);
1413 return;
1414 }
1415 }
1416
1417 // If this is the primary source file, remember that this macro hasn't been
1418 // used yet.
1419 if (isInPrimaryFile())
1420 MI->setIsUsed(false);
1421
1422 MI->setDefinitionEndLoc(LastTok.getLocation());
1423
1424 // Finally, if this identifier already had a macro defined for it, verify that
1425 // the macro bodies are identical and free the old definition.
1426 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
1427 // It is very common for system headers to have tons of macro redefinitions
1428 // and for warnings to be disabled in system headers. If this is the case,
1429 // then don't bother calling MacroInfo::isIdenticalTo.
1430 if (!getDiagnostics().getSuppressSystemWarnings() ||
1431 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
1432 if (!OtherMI->isUsed())
1433 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1434
1435 // Macros must be identical. This means all tokes and whitespace
1436 // separation must be the same. C99 6.10.3.2.
1437 if (!MI->isIdenticalTo(*OtherMI, *this)) {
1438 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
1439 << MacroNameTok.getIdentifierInfo();
1440 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
1441 }
1442 }
1443
1444 ReleaseMacroInfo(OtherMI);
1445 }
1446
1447 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
1448
1449 // If the callbacks want to know, tell them about the macro definition.
1450 if (Callbacks)
1451 Callbacks->MacroDefined(MacroNameTok.getIdentifierInfo(), MI);
1452}
1453
1454/// HandleUndefDirective - Implements #undef.
1455///
1456void Preprocessor::HandleUndefDirective(Token &UndefTok) {
1457 ++NumUndefined;
1458
1459 Token MacroNameTok;
1460 ReadMacroName(MacroNameTok, 2);
1461
1462 // Error reading macro name? If so, diagnostic already issued.
1463 if (MacroNameTok.is(tok::eom))
1464 return;
1465
1466 // Check to see if this is the last token on the #undef line.
1467 CheckEndOfDirective("undef");
1468
1469 // Okay, we finally have a valid identifier to undef.
1470 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
1471
1472 // If the macro is not defined, this is a noop undef, just return.
1473 if (MI == 0) return;
1474
1475 if (!MI->isUsed())
1476 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
1477
1478 // If the callbacks want to know, tell them about the macro #undef.
1479 if (Callbacks)
1480 Callbacks->MacroUndefined(MacroNameTok.getIdentifierInfo(), MI);
1481
1482 // Free macro definition.
1483 ReleaseMacroInfo(MI);
1484 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
1485}
1486
1487
1488//===----------------------------------------------------------------------===//
1489// Preprocessor Conditional Directive Handling.
1490//===----------------------------------------------------------------------===//
1491
1492/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1493/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1494/// if any tokens have been returned or pp-directives activated before this
1495/// #ifndef has been lexed.
1496///
1497void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
1498 bool ReadAnyTokensBeforeDirective) {
1499 ++NumIf;
1500 Token DirectiveTok = Result;
1501
1502 Token MacroNameTok;
1503 ReadMacroName(MacroNameTok);
1504
1505 // Error reading macro name? If so, diagnostic already issued.
1506 if (MacroNameTok.is(tok::eom)) {
1507 // Skip code until we get to #endif. This helps with recovery by not
1508 // emitting an error when the #endif is reached.
1509 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1510 /*Foundnonskip*/false, /*FoundElse*/false);
1511 return;
1512 }
1513
1514 // Check to see if this is the last token on the #if[n]def line.
1515 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
1516
1517 if (CurPPLexer->getConditionalStackDepth() == 0) {
1518 // If the start of a top-level #ifdef, inform MIOpt.
1519 if (!ReadAnyTokensBeforeDirective) {
1520 assert(isIfndef && "#ifdef shouldn't reach here");
1521 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1522 } else
1523 CurPPLexer->MIOpt.EnterTopLevelConditional();
1524 }
1525
1526 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1527 MacroInfo *MI = getMacroInfo(MII);
1528
1529 // If there is a macro, process it.
1530 if (MI) // Mark it used.
1531 MI->setIsUsed(true);
1532
1533 // Should we include the stuff contained by this directive?
1534 if (!MI == isIfndef) {
1535 // Yes, remember that we are inside a conditional, then lex the next token.
1536 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
1537 /*wasskip*/false, /*foundnonskip*/true,
1538 /*foundelse*/false);
1539 } else {
1540 // No, skip the contents of this block and return the first token after it.
1541 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1542 /*Foundnonskip*/false,
1543 /*FoundElse*/false);
1544 }
1545}
1546
1547/// HandleIfDirective - Implements the #if directive.
1548///
1549void Preprocessor::HandleIfDirective(Token &IfToken,
1550 bool ReadAnyTokensBeforeDirective) {
1551 ++NumIf;
1552
1553 // Parse and evaluation the conditional expression.
1554 IdentifierInfo *IfNDefMacro = 0;
1555 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
1556
1557
1558 // If this condition is equivalent to #ifndef X, and if this is the first
1559 // directive seen, handle it for the multiple-include optimization.
1560 if (CurPPLexer->getConditionalStackDepth() == 0) {
1561 if (!ReadAnyTokensBeforeDirective && IfNDefMacro)
1562 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
1563 else
1564 CurPPLexer->MIOpt.EnterTopLevelConditional();
1565 }
1566
1567 // Should we include the stuff contained by this directive?
1568 if (ConditionalTrue) {
1569 // Yes, remember that we are inside a conditional, then lex the next token.
1570 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
1571 /*foundnonskip*/true, /*foundelse*/false);
1572 } else {
1573 // No, skip the contents of this block and return the first token after it.
1574 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
1575 /*FoundElse*/false);
1576 }
1577}
1578
1579/// HandleEndifDirective - Implements the #endif directive.
1580///
1581void Preprocessor::HandleEndifDirective(Token &EndifToken) {
1582 ++NumEndif;
1583
1584 // Check that this is the whole directive.
1585 CheckEndOfDirective("endif");
1586
1587 PPConditionalInfo CondInfo;
1588 if (CurPPLexer->popConditionalLevel(CondInfo)) {
1589 // No conditionals on the stack: this is an #endif without an #if.
1590 Diag(EndifToken, diag::err_pp_endif_without_if);
1591 return;
1592 }
1593
1594 // If this the end of a top-level #endif, inform MIOpt.
1595 if (CurPPLexer->getConditionalStackDepth() == 0)
1596 CurPPLexer->MIOpt.ExitTopLevelConditional();
1597
1598 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
1599 "This code should only be reachable in the non-skipping case!");
1600}
1601
1602
1603void Preprocessor::HandleElseDirective(Token &Result) {
1604 ++NumElse;
1605
1606 // #else directive in a non-skipping conditional... start skipping.
1607 CheckEndOfDirective("else");
1608
1609 PPConditionalInfo CI;
1610 if (CurPPLexer->popConditionalLevel(CI)) {
1611 Diag(Result, diag::pp_err_else_without_if);
1612 return;
1613 }
1614
1615 // If this is a top-level #else, inform the MIOpt.
1616 if (CurPPLexer->getConditionalStackDepth() == 0)
1617 CurPPLexer->MIOpt.EnterTopLevelConditional();
1618
1619 // If this is a #else with a #else before it, report the error.
1620 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
1621
1622 // Finally, skip the rest of the contents of this block and return the first
1623 // token after it.
1624 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1625 /*FoundElse*/true);
1626}
1627
1628void Preprocessor::HandleElifDirective(Token &ElifToken) {
1629 ++NumElse;
1630
1631 // #elif directive in a non-skipping conditional... start skipping.
1632 // We don't care what the condition is, because we will always skip it (since
1633 // the block immediately before it was included).
1634 DiscardUntilEndOfDirective();
1635
1636 PPConditionalInfo CI;
1637 if (CurPPLexer->popConditionalLevel(CI)) {
1638 Diag(ElifToken, diag::pp_err_elif_without_if);
1639 return;
1640 }
1641
1642 // If this is a top-level #elif, inform the MIOpt.
1643 if (CurPPLexer->getConditionalStackDepth() == 0)
1644 CurPPLexer->MIOpt.EnterTopLevelConditional();
1645
1646 // If this is a #elif with a #else before it, report the error.
1647 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
1648
1649 // Finally, skip the rest of the contents of this block and return the first
1650 // token after it.
1651 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1652 /*FoundElse*/CI.FoundElse);
1653}
1654