blob: 2aa8eaedd432226e7e2321a2658105047fa592b4 [file] [log] [blame]
Chris Lattnerc7a39682008-03-09 03:13:06 +00001//===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
Chris Lattner01ee2462008-03-09 01:54:53 +00002//
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/HeaderSearch.h"
16#include "clang/Lex/MacroInfo.h"
17#include "clang/Lex/PPCallbacks.h"
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/SourceManager.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Utility Methods for Preprocessor Directive Handling.
24//===----------------------------------------------------------------------===//
25
26/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
27/// current line until the tok::eom token is found.
28void Preprocessor::DiscardUntilEndOfDirective() {
29 Token Tmp;
30 do {
31 LexUnexpandedToken(Tmp);
32 } while (Tmp.isNot(tok::eom));
33}
34
35/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
36static bool isCXXNamedOperator(const std::string &Spelling) {
37 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
38 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
39 Spelling == "or" || Spelling == "xor";
40}
41
42/// ReadMacroName - Lex and validate a macro name, which occurs after a
43/// #define or #undef. This sets the token kind to eom and discards the rest
44/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
45/// this is due to a a #define, 2 if #undef directive, 0 if it is something
46/// else (e.g. #ifdef).
47void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
48 // Read the token, don't allow macro expansion on it.
49 LexUnexpandedToken(MacroNameTok);
50
51 // Missing macro name?
52 if (MacroNameTok.is(tok::eom))
53 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
54
55 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
56 if (II == 0) {
57 std::string Spelling = getSpelling(MacroNameTok);
58 if (isCXXNamedOperator(Spelling))
59 // C++ 2.5p2: Alternative tokens behave the same as its primary token
60 // except for their spellings.
61 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
62 else
63 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
64 // Fall through on error.
65 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
66 // Error if defining "defined": C99 6.10.8.4.
67 Diag(MacroNameTok, diag::err_defined_macro_name);
68 } else if (isDefineUndef && II->hasMacroDefinition() &&
69 getMacroInfo(II)->isBuiltinMacro()) {
70 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
71 if (isDefineUndef == 1)
72 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
73 else
74 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
75 } else {
76 // Okay, we got a good identifier node. Return it.
77 return;
78 }
79
80 // Invalid macro name, read and discard the rest of the line. Then set the
81 // token kind to tok::eom.
82 MacroNameTok.setKind(tok::eom);
83 return DiscardUntilEndOfDirective();
84}
85
86/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
87/// not, emit a diagnostic and consume up until the eom.
88void Preprocessor::CheckEndOfDirective(const char *DirType) {
89 Token Tmp;
90 // Lex unexpanded tokens: macros might expand to zero tokens, causing us to
91 // miss diagnosing invalid lines.
92 LexUnexpandedToken(Tmp);
93
94 // There should be no tokens after the directive, but we allow them as an
95 // extension.
96 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
97 LexUnexpandedToken(Tmp);
98
99 if (Tmp.isNot(tok::eom)) {
100 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
101 DiscardUntilEndOfDirective();
102 }
103}
104
105
106
107/// SkipExcludedConditionalBlock - We just read a #if or related directive and
108/// decided that the subsequent tokens are in the #if'd out portion of the
109/// file. Lex the rest of the file, until we see an #endif. If
110/// FoundNonSkipPortion is true, then we have already emitted code for part of
111/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
112/// is true, then #else directives are ok, if not, then we have already seen one
113/// so a #else directive is a duplicate. When this returns, the caller can lex
114/// the first valid token.
115void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
116 bool FoundNonSkipPortion,
117 bool FoundElse) {
118 ++NumSkipped;
Chris Lattner5b54ed92008-03-09 02:26:03 +0000119 assert(CurTokenLexer == 0 && CurLexer &&
Chris Lattner01ee2462008-03-09 01:54:53 +0000120 "Lexing a macro, not a file?");
121
122 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
123 FoundNonSkipPortion, FoundElse);
124
125 // Enter raw mode to disable identifier lookup (and thus macro expansion),
126 // disabling warnings, etc.
127 CurLexer->LexingRawMode = true;
128 Token Tok;
129 while (1) {
130 CurLexer->Lex(Tok);
131
132 // If this is the end of the buffer, we have an error.
133 if (Tok.is(tok::eof)) {
134 // Emit errors for each unterminated conditional on the stack, including
135 // the current one.
136 while (!CurLexer->ConditionalStack.empty()) {
137 Diag(CurLexer->ConditionalStack.back().IfLoc,
138 diag::err_pp_unterminated_conditional);
139 CurLexer->ConditionalStack.pop_back();
140 }
141
142 // Just return and let the caller lex after this #include.
143 break;
144 }
145
146 // If this token is not a preprocessor directive, just skip it.
147 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
148 continue;
149
150 // We just parsed a # character at the start of a line, so we're in
151 // directive mode. Tell the lexer this so any newlines we see will be
152 // converted into an EOM token (this terminates the macro).
153 CurLexer->ParsingPreprocessorDirective = true;
154 CurLexer->KeepCommentMode = false;
155
156
157 // Read the next token, the directive flavor.
158 LexUnexpandedToken(Tok);
159
160 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
161 // something bogus), skip it.
162 if (Tok.isNot(tok::identifier)) {
163 CurLexer->ParsingPreprocessorDirective = false;
164 // Restore comment saving mode.
165 CurLexer->KeepCommentMode = KeepComments;
166 continue;
167 }
168
169 // If the first letter isn't i or e, it isn't intesting to us. We know that
170 // this is safe in the face of spelling differences, because there is no way
171 // to spell an i/e in a strange way that is another letter. Skipping this
172 // allows us to avoid looking up the identifier info for #define/#undef and
173 // other common directives.
174 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
175 char FirstChar = RawCharData[0];
176 if (FirstChar >= 'a' && FirstChar <= 'z' &&
177 FirstChar != 'i' && FirstChar != 'e') {
178 CurLexer->ParsingPreprocessorDirective = false;
179 // Restore comment saving mode.
180 CurLexer->KeepCommentMode = KeepComments;
181 continue;
182 }
183
184 // Get the identifier name without trigraphs or embedded newlines. Note
185 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
186 // when skipping.
187 // TODO: could do this with zero copies in the no-clean case by using
188 // strncmp below.
189 char Directive[20];
190 unsigned IdLen;
191 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
192 IdLen = Tok.getLength();
193 memcpy(Directive, RawCharData, IdLen);
194 Directive[IdLen] = 0;
195 } else {
196 std::string DirectiveStr = getSpelling(Tok);
197 IdLen = DirectiveStr.size();
198 if (IdLen >= 20) {
199 CurLexer->ParsingPreprocessorDirective = false;
200 // Restore comment saving mode.
201 CurLexer->KeepCommentMode = KeepComments;
202 continue;
203 }
204 memcpy(Directive, &DirectiveStr[0], IdLen);
205 Directive[IdLen] = 0;
206 }
207
208 if (FirstChar == 'i' && Directive[1] == 'f') {
209 if ((IdLen == 2) || // "if"
210 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
211 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
212 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
213 // bother parsing the condition.
214 DiscardUntilEndOfDirective();
215 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
216 /*foundnonskip*/false,
217 /*fnddelse*/false);
218 }
219 } else if (FirstChar == 'e') {
220 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
221 CheckEndOfDirective("#endif");
222 PPConditionalInfo CondInfo;
223 CondInfo.WasSkipping = true; // Silence bogus warning.
224 bool InCond = CurLexer->popConditionalLevel(CondInfo);
225 InCond = InCond; // Silence warning in no-asserts mode.
226 assert(!InCond && "Can't be skipping if not in a conditional!");
227
228 // If we popped the outermost skipping block, we're done skipping!
229 if (!CondInfo.WasSkipping)
230 break;
231 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
232 // #else directive in a skipping conditional. If not in some other
233 // skipping conditional, and if #else hasn't already been seen, enter it
234 // as a non-skipping conditional.
235 CheckEndOfDirective("#else");
236 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
237
238 // If this is a #else with a #else before it, report the error.
239 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
240
241 // Note that we've seen a #else in this conditional.
242 CondInfo.FoundElse = true;
243
244 // If the conditional is at the top level, and the #if block wasn't
245 // entered, enter the #else block now.
246 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
247 CondInfo.FoundNonSkip = true;
248 break;
249 }
250 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
251 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
252
253 bool ShouldEnter;
254 // If this is in a skipping block or if we're already handled this #if
255 // block, don't bother parsing the condition.
256 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
257 DiscardUntilEndOfDirective();
258 ShouldEnter = false;
259 } else {
260 // Restore the value of LexingRawMode so that identifiers are
261 // looked up, etc, inside the #elif expression.
262 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
263 CurLexer->LexingRawMode = false;
264 IdentifierInfo *IfNDefMacro = 0;
265 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
266 CurLexer->LexingRawMode = true;
267 }
268
269 // If this is a #elif with a #else before it, report the error.
270 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
271
272 // If this condition is true, enter it!
273 if (ShouldEnter) {
274 CondInfo.FoundNonSkip = true;
275 break;
276 }
277 }
278 }
279
280 CurLexer->ParsingPreprocessorDirective = false;
281 // Restore comment saving mode.
282 CurLexer->KeepCommentMode = KeepComments;
283 }
284
285 // Finally, if we are out of the conditional (saw an #endif or ran off the end
286 // of the file, just stop skipping and return to lexing whatever came after
287 // the #if block.
288 CurLexer->LexingRawMode = false;
289}
290
Chris Lattner957c7d82008-03-09 04:17:44 +0000291/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
292/// return null on failure. isAngled indicates whether the file reference is
293/// for system #include's or not (i.e. using <> instead of "").
294const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
295 const char *FilenameEnd,
296 bool isAngled,
297 const DirectoryLookup *FromDir,
298 const DirectoryLookup *&CurDir) {
299 // If the header lookup mechanism may be relative to the current file, pass in
300 // info about where the current file is.
301 const FileEntry *CurFileEnt = 0;
302 if (!FromDir) {
303 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
304 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
305 }
306
307 // Do a standard file entry lookup.
308 CurDir = CurDirLookup;
309 const FileEntry *FE =
310 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
311 isAngled, FromDir, CurDir, CurFileEnt);
312 if (FE) return FE;
313
314 // Otherwise, see if this is a subframework header. If so, this is relative
315 // to one of the headers on the #include stack. Walk the list of the current
316 // headers on the #include stack and pass them to HeaderInfo.
317 if (CurLexer && !CurLexer->Is_PragmaLexer) {
318 if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())))
319 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
320 CurFileEnt)))
321 return FE;
322 }
323
324 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
325 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
326 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
327 if ((CurFileEnt =
328 SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc())))
329 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
330 FilenameEnd, CurFileEnt)))
331 return FE;
332 }
333 }
334
335 // Otherwise, we really couldn't find the file.
336 return 0;
337}
338
Chris Lattner01ee2462008-03-09 01:54:53 +0000339
340//===----------------------------------------------------------------------===//
341// Preprocessor Directive Handling.
342//===----------------------------------------------------------------------===//
343
344/// HandleDirective - This callback is invoked when the lexer sees a # token
345/// at the start of a line. This consumes the directive, modifies the
346/// lexer/preprocessor state, and advances the lexer(s) so that the next token
347/// read is the correct one.
348void Preprocessor::HandleDirective(Token &Result) {
349 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
350
351 // We just parsed a # character at the start of a line, so we're in directive
352 // mode. Tell the lexer this so any newlines we see will be converted into an
353 // EOM token (which terminates the directive).
354 CurLexer->ParsingPreprocessorDirective = true;
355
356 ++NumDirectives;
357
358 // We are about to read a token. For the multiple-include optimization FA to
359 // work, we have to remember if we had read any tokens *before* this
360 // pp-directive.
361 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
362
363 // Read the next token, the directive flavor. This isn't expanded due to
364 // C99 6.10.3p8.
365 LexUnexpandedToken(Result);
366
367 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
368 // #define A(x) #x
369 // A(abc
370 // #warning blah
371 // def)
372 // If so, the user is relying on non-portable behavior, emit a diagnostic.
373 if (InMacroArgs)
374 Diag(Result, diag::ext_embedded_directive);
375
376TryAgain:
377 switch (Result.getKind()) {
378 case tok::eom:
379 return; // null directive.
380 case tok::comment:
381 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
382 LexUnexpandedToken(Result);
383 goto TryAgain;
384
385 case tok::numeric_constant:
386 // FIXME: implement # 7 line numbers!
387 DiscardUntilEndOfDirective();
388 return;
389 default:
390 IdentifierInfo *II = Result.getIdentifierInfo();
391 if (II == 0) break; // Not an identifier.
392
393 // Ask what the preprocessor keyword ID is.
394 switch (II->getPPKeywordID()) {
395 default: break;
396 // C99 6.10.1 - Conditional Inclusion.
397 case tok::pp_if:
398 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
399 case tok::pp_ifdef:
400 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
401 case tok::pp_ifndef:
402 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
403 case tok::pp_elif:
404 return HandleElifDirective(Result);
405 case tok::pp_else:
406 return HandleElseDirective(Result);
407 case tok::pp_endif:
408 return HandleEndifDirective(Result);
409
410 // C99 6.10.2 - Source File Inclusion.
411 case tok::pp_include:
412 return HandleIncludeDirective(Result); // Handle #include.
413
414 // C99 6.10.3 - Macro Replacement.
415 case tok::pp_define:
416 return HandleDefineDirective(Result);
417 case tok::pp_undef:
418 return HandleUndefDirective(Result);
419
420 // C99 6.10.4 - Line Control.
421 case tok::pp_line:
422 // FIXME: implement #line
423 DiscardUntilEndOfDirective();
424 return;
425
426 // C99 6.10.5 - Error Directive.
427 case tok::pp_error:
428 return HandleUserDiagnosticDirective(Result, false);
429
430 // C99 6.10.6 - Pragma Directive.
431 case tok::pp_pragma:
432 return HandlePragmaDirective();
433
434 // GNU Extensions.
435 case tok::pp_import:
436 return HandleImportDirective(Result);
437 case tok::pp_include_next:
438 return HandleIncludeNextDirective(Result);
439
440 case tok::pp_warning:
441 Diag(Result, diag::ext_pp_warning_directive);
442 return HandleUserDiagnosticDirective(Result, true);
443 case tok::pp_ident:
444 return HandleIdentSCCSDirective(Result);
445 case tok::pp_sccs:
446 return HandleIdentSCCSDirective(Result);
447 case tok::pp_assert:
448 //isExtension = true; // FIXME: implement #assert
449 break;
450 case tok::pp_unassert:
451 //isExtension = true; // FIXME: implement #unassert
452 break;
453 }
454 break;
455 }
456
457 // If we reached here, the preprocessing token is not valid!
458 Diag(Result, diag::err_pp_invalid_directive);
459
460 // Read the rest of the PP line.
461 DiscardUntilEndOfDirective();
462
463 // Okay, we're done parsing the directive.
464}
465
466void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
467 bool isWarning) {
468 // Read the rest of the line raw. We do this because we don't want macros
469 // to be expanded and we don't require that the tokens be valid preprocessing
470 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
471 // collapse multiple consequtive white space between tokens, but this isn't
472 // specified by the standard.
473 std::string Message = CurLexer->ReadToEndOfLine();
474
475 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
476 return Diag(Tok, DiagID, Message);
477}
478
479/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
480///
481void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
482 // Yes, this directive is an extension.
483 Diag(Tok, diag::ext_pp_ident_directive);
484
485 // Read the string argument.
486 Token StrTok;
487 Lex(StrTok);
488
489 // If the token kind isn't a string, it's a malformed directive.
490 if (StrTok.isNot(tok::string_literal) &&
491 StrTok.isNot(tok::wide_string_literal))
492 return Diag(StrTok, diag::err_pp_malformed_ident);
493
494 // Verify that there is nothing after the string, other than EOM.
495 CheckEndOfDirective("#ident");
496
497 if (Callbacks)
498 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
499}
500
501//===----------------------------------------------------------------------===//
502// Preprocessor Include Directive Handling.
503//===----------------------------------------------------------------------===//
504
505/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
506/// checked and spelled filename, e.g. as an operand of #include. This returns
507/// true if the input filename was in <>'s or false if it were in ""'s. The
508/// caller is expected to provide a buffer that is large enough to hold the
509/// spelling of the filename, but is also expected to handle the case when
510/// this method decides to use a different buffer.
511bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
512 const char *&BufStart,
513 const char *&BufEnd) {
514 // Get the text form of the filename.
515 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
516
517 // Make sure the filename is <x> or "x".
518 bool isAngled;
519 if (BufStart[0] == '<') {
520 if (BufEnd[-1] != '>') {
521 Diag(Loc, diag::err_pp_expects_filename);
522 BufStart = 0;
523 return true;
524 }
525 isAngled = true;
526 } else if (BufStart[0] == '"') {
527 if (BufEnd[-1] != '"') {
528 Diag(Loc, diag::err_pp_expects_filename);
529 BufStart = 0;
530 return true;
531 }
532 isAngled = false;
533 } else {
534 Diag(Loc, diag::err_pp_expects_filename);
535 BufStart = 0;
536 return true;
537 }
538
539 // Diagnose #include "" as invalid.
540 if (BufEnd-BufStart <= 2) {
541 Diag(Loc, diag::err_pp_empty_filename);
542 BufStart = 0;
543 return "";
544 }
545
546 // Skip the brackets.
547 ++BufStart;
548 --BufEnd;
549 return isAngled;
550}
551
552/// ConcatenateIncludeName - Handle cases where the #include name is expanded
553/// from a macro as multiple tokens, which need to be glued together. This
554/// occurs for code like:
555/// #define FOO <a/b.h>
556/// #include FOO
557/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
558///
559/// This code concatenates and consumes tokens up to the '>' token. It returns
560/// false if the > was found, otherwise it returns true if it finds and consumes
561/// the EOM marker.
562static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
563 Preprocessor &PP) {
564 Token CurTok;
565
566 PP.Lex(CurTok);
567 while (CurTok.isNot(tok::eom)) {
568 // Append the spelling of this token to the buffer. If there was a space
569 // before it, add it now.
570 if (CurTok.hasLeadingSpace())
571 FilenameBuffer.push_back(' ');
572
573 // Get the spelling of the token, directly into FilenameBuffer if possible.
574 unsigned PreAppendSize = FilenameBuffer.size();
575 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
576
577 const char *BufPtr = &FilenameBuffer[PreAppendSize];
578 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
579
580 // If the token was spelled somewhere else, copy it into FilenameBuffer.
581 if (BufPtr != &FilenameBuffer[PreAppendSize])
582 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
583
584 // Resize FilenameBuffer to the correct size.
585 if (CurTok.getLength() != ActualLen)
586 FilenameBuffer.resize(PreAppendSize+ActualLen);
587
588 // If we found the '>' marker, return success.
589 if (CurTok.is(tok::greater))
590 return false;
591
592 PP.Lex(CurTok);
593 }
594
595 // If we hit the eom marker, emit an error and return true so that the caller
596 // knows the EOM has been read.
597 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
598 return true;
599}
600
601/// HandleIncludeDirective - The "#include" tokens have just been read, read the
602/// file to be included from the lexer, then include it! This is a common
603/// routine with functionality shared between #include, #include_next and
604/// #import.
605void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
606 const DirectoryLookup *LookupFrom,
607 bool isImport) {
608
609 Token FilenameTok;
610 CurLexer->LexIncludeFilename(FilenameTok);
611
612 // Reserve a buffer to get the spelling.
613 llvm::SmallVector<char, 128> FilenameBuffer;
614 const char *FilenameStart, *FilenameEnd;
615
616 switch (FilenameTok.getKind()) {
617 case tok::eom:
618 // If the token kind is EOM, the error has already been diagnosed.
619 return;
620
621 case tok::angle_string_literal:
622 case tok::string_literal: {
623 FilenameBuffer.resize(FilenameTok.getLength());
624 FilenameStart = &FilenameBuffer[0];
625 unsigned Len = getSpelling(FilenameTok, FilenameStart);
626 FilenameEnd = FilenameStart+Len;
627 break;
628 }
629
630 case tok::less:
631 // This could be a <foo/bar.h> file coming from a macro expansion. In this
632 // case, glue the tokens together into FilenameBuffer and interpret those.
633 FilenameBuffer.push_back('<');
634 if (ConcatenateIncludeName(FilenameBuffer, *this))
635 return; // Found <eom> but no ">"? Diagnostic already emitted.
636 FilenameStart = &FilenameBuffer[0];
637 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
638 break;
639 default:
640 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
641 DiscardUntilEndOfDirective();
642 return;
643 }
644
645 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
646 FilenameStart, FilenameEnd);
647 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
648 // error.
649 if (FilenameStart == 0) {
650 DiscardUntilEndOfDirective();
651 return;
652 }
653
654 // Verify that there is nothing after the filename, other than EOM. Use the
655 // preprocessor to lex this in case lexing the filename entered a macro.
656 CheckEndOfDirective("#include");
657
658 // Check that we don't have infinite #include recursion.
659 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
660 return Diag(FilenameTok, diag::err_pp_include_too_deep);
661
662 // Search include directories.
663 const DirectoryLookup *CurDir;
664 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
665 isAngled, LookupFrom, CurDir);
666 if (File == 0)
667 return Diag(FilenameTok, diag::err_pp_file_not_found,
668 std::string(FilenameStart, FilenameEnd));
669
670 // Ask HeaderInfo if we should enter this #include file.
671 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
672 // If it returns true, #including this file will have no effect.
673 return;
674 }
675
676 // Look up the file, create a File ID for it.
Nico Weberd2a6ac92008-08-10 19:59:06 +0000677 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation(),
678 isSystemHeader(File));
Chris Lattner01ee2462008-03-09 01:54:53 +0000679 if (FileID == 0)
680 return Diag(FilenameTok, diag::err_pp_file_not_found,
681 std::string(FilenameStart, FilenameEnd));
682
683 // Finally, if all is good, enter the new file!
684 EnterSourceFile(FileID, CurDir);
685}
686
687/// HandleIncludeNextDirective - Implements #include_next.
688///
689void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
690 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
691
692 // #include_next is like #include, except that we start searching after
693 // the current found directory. If we can't do this, issue a
694 // diagnostic.
695 const DirectoryLookup *Lookup = CurDirLookup;
696 if (isInPrimaryFile()) {
697 Lookup = 0;
698 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
699 } else if (Lookup == 0) {
700 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
701 } else {
702 // Start looking up in the next directory.
703 ++Lookup;
704 }
705
706 return HandleIncludeDirective(IncludeNextTok, Lookup);
707}
708
709/// HandleImportDirective - Implements #import.
710///
711void Preprocessor::HandleImportDirective(Token &ImportTok) {
712 Diag(ImportTok, diag::ext_pp_import_directive);
713
714 return HandleIncludeDirective(ImportTok, 0, true);
715}
716
717//===----------------------------------------------------------------------===//
718// Preprocessor Macro Directive Handling.
719//===----------------------------------------------------------------------===//
720
721/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
722/// definition has just been read. Lex the rest of the arguments and the
723/// closing ), updating MI with what we learn. Return true if an error occurs
724/// parsing the arg list.
725bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
726 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
727
728 Token Tok;
729 while (1) {
730 LexUnexpandedToken(Tok);
731 switch (Tok.getKind()) {
732 case tok::r_paren:
733 // Found the end of the argument list.
734 if (Arguments.empty()) { // #define FOO()
735 MI->setArgumentList(Arguments.begin(), Arguments.end());
736 return false;
737 }
738 // Otherwise we have #define FOO(A,)
739 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
740 return true;
741 case tok::ellipsis: // #define X(... -> C99 varargs
742 // Warn if use of C99 feature in non-C99 mode.
743 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
744
745 // Lex the token after the identifier.
746 LexUnexpandedToken(Tok);
747 if (Tok.isNot(tok::r_paren)) {
748 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
749 return true;
750 }
751 // Add the __VA_ARGS__ identifier as an argument.
752 Arguments.push_back(Ident__VA_ARGS__);
753 MI->setIsC99Varargs();
754 MI->setArgumentList(Arguments.begin(), Arguments.end());
755 return false;
756 case tok::eom: // #define X(
757 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
758 return true;
759 default:
760 // Handle keywords and identifiers here to accept things like
761 // #define Foo(for) for.
762 IdentifierInfo *II = Tok.getIdentifierInfo();
763 if (II == 0) {
764 // #define X(1
765 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
766 return true;
767 }
768
769 // If this is already used as an argument, it is used multiple times (e.g.
770 // #define X(A,A.
771 if (std::find(Arguments.begin(), Arguments.end(), II) !=
772 Arguments.end()) { // C99 6.10.3p6
773 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
774 return true;
775 }
776
777 // Add the argument to the macro info.
778 Arguments.push_back(II);
779
780 // Lex the token after the identifier.
781 LexUnexpandedToken(Tok);
782
783 switch (Tok.getKind()) {
784 default: // #define X(A B
785 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
786 return true;
787 case tok::r_paren: // #define X(A)
788 MI->setArgumentList(Arguments.begin(), Arguments.end());
789 return false;
790 case tok::comma: // #define X(A,
791 break;
792 case tok::ellipsis: // #define X(A... -> GCC extension
793 // Diagnose extension.
794 Diag(Tok, diag::ext_named_variadic_macro);
795
796 // Lex the token after the identifier.
797 LexUnexpandedToken(Tok);
798 if (Tok.isNot(tok::r_paren)) {
799 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
800 return true;
801 }
802
803 MI->setIsGNUVarargs();
804 MI->setArgumentList(Arguments.begin(), Arguments.end());
805 return false;
806 }
807 }
808 }
809}
810
811/// HandleDefineDirective - Implements #define. This consumes the entire macro
812/// line then lets the caller lex the next real token.
813void Preprocessor::HandleDefineDirective(Token &DefineTok) {
814 ++NumDefined;
815
816 Token MacroNameTok;
817 ReadMacroName(MacroNameTok, 1);
818
819 // Error reading macro name? If so, diagnostic already issued.
820 if (MacroNameTok.is(tok::eom))
821 return;
822
823 // If we are supposed to keep comments in #defines, reenable comment saving
824 // mode.
825 CurLexer->KeepCommentMode = KeepMacroComments;
826
827 // Create the new macro.
828 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
829
830 Token Tok;
831 LexUnexpandedToken(Tok);
832
833 // If this is a function-like macro definition, parse the argument list,
834 // marking each of the identifiers as being used as macro arguments. Also,
835 // check other constraints on the first token of the macro body.
836 if (Tok.is(tok::eom)) {
837 // If there is no body to this macro, we have no special handling here.
838 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
839 // This is a function-like macro definition. Read the argument list.
840 MI->setIsFunctionLike();
841 if (ReadMacroDefinitionArgList(MI)) {
842 // Forget about MI.
843 delete MI;
844 // Throw away the rest of the line.
845 if (CurLexer->ParsingPreprocessorDirective)
846 DiscardUntilEndOfDirective();
847 return;
848 }
849
850 // Read the first token after the arg list for down below.
851 LexUnexpandedToken(Tok);
852 } else if (!Tok.hasLeadingSpace()) {
853 // C99 requires whitespace between the macro definition and the body. Emit
854 // a diagnostic for something like "#define X+".
855 if (Features.C99) {
856 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
857 } else {
858 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
859 // one in some cases!
860 }
861 } else {
862 // This is a normal token with leading space. Clear the leading space
863 // marker on the first token to get proper expansion.
864 Tok.clearFlag(Token::LeadingSpace);
865 }
866
867 // If this is a definition of a variadic C99 function-like macro, not using
868 // the GNU named varargs extension, enabled __VA_ARGS__.
869
870 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
871 // This gets unpoisoned where it is allowed.
872 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
873 if (MI->isC99Varargs())
874 Ident__VA_ARGS__->setIsPoisoned(false);
875
876 // Read the rest of the macro body.
877 if (MI->isObjectLike()) {
878 // Object-like macros are very simple, just read their body.
879 while (Tok.isNot(tok::eom)) {
880 MI->AddTokenToBody(Tok);
881 // Get the next token of the macro.
882 LexUnexpandedToken(Tok);
883 }
884
885 } else {
886 // Otherwise, read the body of a function-like macro. This has to validate
887 // the # (stringize) operator.
888 while (Tok.isNot(tok::eom)) {
889 MI->AddTokenToBody(Tok);
890
891 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
892 // parameters in function-like macro expansions.
893 if (Tok.isNot(tok::hash)) {
894 // Get the next token of the macro.
895 LexUnexpandedToken(Tok);
896 continue;
897 }
898
899 // Get the next token of the macro.
900 LexUnexpandedToken(Tok);
901
902 // Not a macro arg identifier?
903 if (!Tok.getIdentifierInfo() ||
904 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
905 Diag(Tok, diag::err_pp_stringize_not_parameter);
906 delete MI;
907
908 // Disable __VA_ARGS__ again.
909 Ident__VA_ARGS__->setIsPoisoned(true);
910 return;
911 }
912
913 // Things look ok, add the param name token to the macro.
914 MI->AddTokenToBody(Tok);
915
916 // Get the next token of the macro.
917 LexUnexpandedToken(Tok);
918 }
919 }
920
921
922 // Disable __VA_ARGS__ again.
923 Ident__VA_ARGS__->setIsPoisoned(true);
924
925 // Check that there is no paste (##) operator at the begining or end of the
926 // replacement list.
927 unsigned NumTokens = MI->getNumTokens();
928 if (NumTokens != 0) {
929 if (MI->getReplacementToken(0).is(tok::hashhash)) {
930 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
931 delete MI;
932 return;
933 }
934 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
935 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
936 delete MI;
937 return;
938 }
939 }
940
941 // If this is the primary source file, remember that this macro hasn't been
942 // used yet.
943 if (isInPrimaryFile())
944 MI->setIsUsed(false);
945
946 // Finally, if this identifier already had a macro defined for it, verify that
947 // the macro bodies are identical and free the old definition.
948 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
949 if (!OtherMI->isUsed())
950 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
951
952 // Macros must be identical. This means all tokes and whitespace separation
953 // must be the same. C99 6.10.3.2.
954 if (!MI->isIdenticalTo(*OtherMI, *this)) {
955 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
956 MacroNameTok.getIdentifierInfo()->getName());
957 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
958 }
959 delete OtherMI;
960 }
961
962 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
963}
964
965/// HandleUndefDirective - Implements #undef.
966///
967void Preprocessor::HandleUndefDirective(Token &UndefTok) {
968 ++NumUndefined;
969
970 Token MacroNameTok;
971 ReadMacroName(MacroNameTok, 2);
972
973 // Error reading macro name? If so, diagnostic already issued.
974 if (MacroNameTok.is(tok::eom))
975 return;
976
977 // Check to see if this is the last token on the #undef line.
978 CheckEndOfDirective("#undef");
979
980 // Okay, we finally have a valid identifier to undef.
981 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
982
983 // If the macro is not defined, this is a noop undef, just return.
984 if (MI == 0) return;
985
986 if (!MI->isUsed())
987 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
988
989 // Free macro definition.
990 delete MI;
991 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
992}
993
994
995//===----------------------------------------------------------------------===//
996// Preprocessor Conditional Directive Handling.
997//===----------------------------------------------------------------------===//
998
999/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1000/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1001/// if any tokens have been returned or pp-directives activated before this
1002/// #ifndef has been lexed.
1003///
1004void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
1005 bool ReadAnyTokensBeforeDirective) {
1006 ++NumIf;
1007 Token DirectiveTok = Result;
1008
1009 Token MacroNameTok;
1010 ReadMacroName(MacroNameTok);
1011
1012 // Error reading macro name? If so, diagnostic already issued.
1013 if (MacroNameTok.is(tok::eom)) {
1014 // Skip code until we get to #endif. This helps with recovery by not
1015 // emitting an error when the #endif is reached.
1016 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1017 /*Foundnonskip*/false, /*FoundElse*/false);
1018 return;
1019 }
1020
1021 // Check to see if this is the last token on the #if[n]def line.
1022 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
1023
1024 if (CurLexer->getConditionalStackDepth() == 0) {
1025 // If the start of a top-level #ifdef, inform MIOpt.
1026 if (!ReadAnyTokensBeforeDirective) {
1027 assert(isIfndef && "#ifdef shouldn't reach here");
1028 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1029 } else
1030 CurLexer->MIOpt.EnterTopLevelConditional();
1031 }
1032
1033 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1034 MacroInfo *MI = getMacroInfo(MII);
1035
1036 // If there is a macro, process it.
1037 if (MI) // Mark it used.
1038 MI->setIsUsed(true);
1039
1040 // Should we include the stuff contained by this directive?
1041 if (!MI == isIfndef) {
1042 // Yes, remember that we are inside a conditional, then lex the next token.
1043 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
1044 /*foundnonskip*/true, /*foundelse*/false);
1045 } else {
1046 // No, skip the contents of this block and return the first token after it.
1047 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1048 /*Foundnonskip*/false,
1049 /*FoundElse*/false);
1050 }
1051}
1052
1053/// HandleIfDirective - Implements the #if directive.
1054///
1055void Preprocessor::HandleIfDirective(Token &IfToken,
1056 bool ReadAnyTokensBeforeDirective) {
1057 ++NumIf;
1058
1059 // Parse and evaluation the conditional expression.
1060 IdentifierInfo *IfNDefMacro = 0;
1061 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
1062
Nuno Lopes7ccd24d2008-06-01 18:31:24 +00001063
1064 // If this condition is equivalent to #ifndef X, and if this is the first
1065 // directive seen, handle it for the multiple-include optimization.
1066 if (CurLexer->getConditionalStackDepth() == 0) {
1067 if (!ReadAnyTokensBeforeDirective && IfNDefMacro)
1068 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
1069 else
1070 CurLexer->MIOpt.EnterTopLevelConditional();
1071 }
1072
Chris Lattner01ee2462008-03-09 01:54:53 +00001073 // Should we include the stuff contained by this directive?
1074 if (ConditionalTrue) {
Chris Lattner01ee2462008-03-09 01:54:53 +00001075 // Yes, remember that we are inside a conditional, then lex the next token.
1076 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
1077 /*foundnonskip*/true, /*foundelse*/false);
1078 } else {
1079 // No, skip the contents of this block and return the first token after it.
1080 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
1081 /*FoundElse*/false);
1082 }
1083}
1084
1085/// HandleEndifDirective - Implements the #endif directive.
1086///
1087void Preprocessor::HandleEndifDirective(Token &EndifToken) {
1088 ++NumEndif;
1089
1090 // Check that this is the whole directive.
1091 CheckEndOfDirective("#endif");
1092
1093 PPConditionalInfo CondInfo;
1094 if (CurLexer->popConditionalLevel(CondInfo)) {
1095 // No conditionals on the stack: this is an #endif without an #if.
1096 return Diag(EndifToken, diag::err_pp_endif_without_if);
1097 }
1098
1099 // If this the end of a top-level #endif, inform MIOpt.
1100 if (CurLexer->getConditionalStackDepth() == 0)
1101 CurLexer->MIOpt.ExitTopLevelConditional();
1102
1103 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
1104 "This code should only be reachable in the non-skipping case!");
1105}
1106
1107
1108void Preprocessor::HandleElseDirective(Token &Result) {
1109 ++NumElse;
1110
1111 // #else directive in a non-skipping conditional... start skipping.
1112 CheckEndOfDirective("#else");
1113
1114 PPConditionalInfo CI;
1115 if (CurLexer->popConditionalLevel(CI))
1116 return Diag(Result, diag::pp_err_else_without_if);
1117
1118 // If this is a top-level #else, inform the MIOpt.
1119 if (CurLexer->getConditionalStackDepth() == 0)
1120 CurLexer->MIOpt.EnterTopLevelConditional();
1121
1122 // If this is a #else with a #else before it, report the error.
1123 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
1124
1125 // Finally, skip the rest of the contents of this block and return the first
1126 // token after it.
1127 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1128 /*FoundElse*/true);
1129}
1130
1131void Preprocessor::HandleElifDirective(Token &ElifToken) {
1132 ++NumElse;
1133
1134 // #elif directive in a non-skipping conditional... start skipping.
1135 // We don't care what the condition is, because we will always skip it (since
1136 // the block immediately before it was included).
1137 DiscardUntilEndOfDirective();
1138
1139 PPConditionalInfo CI;
1140 if (CurLexer->popConditionalLevel(CI))
1141 return Diag(ElifToken, diag::pp_err_elif_without_if);
1142
1143 // If this is a top-level #elif, inform the MIOpt.
1144 if (CurLexer->getConditionalStackDepth() == 0)
1145 CurLexer->MIOpt.EnterTopLevelConditional();
1146
1147 // If this is a #elif with a #else before it, report the error.
1148 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
1149
1150 // Finally, skip the rest of the contents of this block and return the first
1151 // token after it.
1152 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1153 /*FoundElse*/CI.FoundElse);
1154}
1155