blob: 11afa84f3bab9eba46eef6e3a8749eac61d6d217 [file] [log] [blame]
Chris Lattnera3b605e2008-03-09 03:13:06 +00001//===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
Chris Lattner141e71f2008-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 Lattner6cfe7592008-03-09 02:26:03 +0000119 assert(CurTokenLexer == 0 && CurLexer &&
Chris Lattner141e71f2008-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 Lattner10725092008-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 Lattner141e71f2008-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.
677 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
678 if (FileID == 0)
679 return Diag(FilenameTok, diag::err_pp_file_not_found,
680 std::string(FilenameStart, FilenameEnd));
681
682 // Finally, if all is good, enter the new file!
683 EnterSourceFile(FileID, CurDir);
684}
685
686/// HandleIncludeNextDirective - Implements #include_next.
687///
688void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
689 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
690
691 // #include_next is like #include, except that we start searching after
692 // the current found directory. If we can't do this, issue a
693 // diagnostic.
694 const DirectoryLookup *Lookup = CurDirLookup;
695 if (isInPrimaryFile()) {
696 Lookup = 0;
697 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
698 } else if (Lookup == 0) {
699 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
700 } else {
701 // Start looking up in the next directory.
702 ++Lookup;
703 }
704
705 return HandleIncludeDirective(IncludeNextTok, Lookup);
706}
707
708/// HandleImportDirective - Implements #import.
709///
710void Preprocessor::HandleImportDirective(Token &ImportTok) {
711 Diag(ImportTok, diag::ext_pp_import_directive);
712
713 return HandleIncludeDirective(ImportTok, 0, true);
714}
715
716//===----------------------------------------------------------------------===//
717// Preprocessor Macro Directive Handling.
718//===----------------------------------------------------------------------===//
719
720/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
721/// definition has just been read. Lex the rest of the arguments and the
722/// closing ), updating MI with what we learn. Return true if an error occurs
723/// parsing the arg list.
724bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
725 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
726
727 Token Tok;
728 while (1) {
729 LexUnexpandedToken(Tok);
730 switch (Tok.getKind()) {
731 case tok::r_paren:
732 // Found the end of the argument list.
733 if (Arguments.empty()) { // #define FOO()
734 MI->setArgumentList(Arguments.begin(), Arguments.end());
735 return false;
736 }
737 // Otherwise we have #define FOO(A,)
738 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
739 return true;
740 case tok::ellipsis: // #define X(... -> C99 varargs
741 // Warn if use of C99 feature in non-C99 mode.
742 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
743
744 // Lex the token after the identifier.
745 LexUnexpandedToken(Tok);
746 if (Tok.isNot(tok::r_paren)) {
747 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
748 return true;
749 }
750 // Add the __VA_ARGS__ identifier as an argument.
751 Arguments.push_back(Ident__VA_ARGS__);
752 MI->setIsC99Varargs();
753 MI->setArgumentList(Arguments.begin(), Arguments.end());
754 return false;
755 case tok::eom: // #define X(
756 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
757 return true;
758 default:
759 // Handle keywords and identifiers here to accept things like
760 // #define Foo(for) for.
761 IdentifierInfo *II = Tok.getIdentifierInfo();
762 if (II == 0) {
763 // #define X(1
764 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
765 return true;
766 }
767
768 // If this is already used as an argument, it is used multiple times (e.g.
769 // #define X(A,A.
770 if (std::find(Arguments.begin(), Arguments.end(), II) !=
771 Arguments.end()) { // C99 6.10.3p6
772 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
773 return true;
774 }
775
776 // Add the argument to the macro info.
777 Arguments.push_back(II);
778
779 // Lex the token after the identifier.
780 LexUnexpandedToken(Tok);
781
782 switch (Tok.getKind()) {
783 default: // #define X(A B
784 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
785 return true;
786 case tok::r_paren: // #define X(A)
787 MI->setArgumentList(Arguments.begin(), Arguments.end());
788 return false;
789 case tok::comma: // #define X(A,
790 break;
791 case tok::ellipsis: // #define X(A... -> GCC extension
792 // Diagnose extension.
793 Diag(Tok, diag::ext_named_variadic_macro);
794
795 // Lex the token after the identifier.
796 LexUnexpandedToken(Tok);
797 if (Tok.isNot(tok::r_paren)) {
798 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
799 return true;
800 }
801
802 MI->setIsGNUVarargs();
803 MI->setArgumentList(Arguments.begin(), Arguments.end());
804 return false;
805 }
806 }
807 }
808}
809
810/// HandleDefineDirective - Implements #define. This consumes the entire macro
811/// line then lets the caller lex the next real token.
812void Preprocessor::HandleDefineDirective(Token &DefineTok) {
813 ++NumDefined;
814
815 Token MacroNameTok;
816 ReadMacroName(MacroNameTok, 1);
817
818 // Error reading macro name? If so, diagnostic already issued.
819 if (MacroNameTok.is(tok::eom))
820 return;
821
822 // If we are supposed to keep comments in #defines, reenable comment saving
823 // mode.
824 CurLexer->KeepCommentMode = KeepMacroComments;
825
826 // Create the new macro.
827 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
828
829 Token Tok;
830 LexUnexpandedToken(Tok);
831
832 // If this is a function-like macro definition, parse the argument list,
833 // marking each of the identifiers as being used as macro arguments. Also,
834 // check other constraints on the first token of the macro body.
835 if (Tok.is(tok::eom)) {
836 // If there is no body to this macro, we have no special handling here.
837 } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) {
838 // This is a function-like macro definition. Read the argument list.
839 MI->setIsFunctionLike();
840 if (ReadMacroDefinitionArgList(MI)) {
841 // Forget about MI.
842 delete MI;
843 // Throw away the rest of the line.
844 if (CurLexer->ParsingPreprocessorDirective)
845 DiscardUntilEndOfDirective();
846 return;
847 }
848
849 // Read the first token after the arg list for down below.
850 LexUnexpandedToken(Tok);
851 } else if (!Tok.hasLeadingSpace()) {
852 // C99 requires whitespace between the macro definition and the body. Emit
853 // a diagnostic for something like "#define X+".
854 if (Features.C99) {
855 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
856 } else {
857 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
858 // one in some cases!
859 }
860 } else {
861 // This is a normal token with leading space. Clear the leading space
862 // marker on the first token to get proper expansion.
863 Tok.clearFlag(Token::LeadingSpace);
864 }
865
866 // If this is a definition of a variadic C99 function-like macro, not using
867 // the GNU named varargs extension, enabled __VA_ARGS__.
868
869 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
870 // This gets unpoisoned where it is allowed.
871 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
872 if (MI->isC99Varargs())
873 Ident__VA_ARGS__->setIsPoisoned(false);
874
875 // Read the rest of the macro body.
876 if (MI->isObjectLike()) {
877 // Object-like macros are very simple, just read their body.
878 while (Tok.isNot(tok::eom)) {
879 MI->AddTokenToBody(Tok);
880 // Get the next token of the macro.
881 LexUnexpandedToken(Tok);
882 }
883
884 } else {
885 // Otherwise, read the body of a function-like macro. This has to validate
886 // the # (stringize) operator.
887 while (Tok.isNot(tok::eom)) {
888 MI->AddTokenToBody(Tok);
889
890 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
891 // parameters in function-like macro expansions.
892 if (Tok.isNot(tok::hash)) {
893 // Get the next token of the macro.
894 LexUnexpandedToken(Tok);
895 continue;
896 }
897
898 // Get the next token of the macro.
899 LexUnexpandedToken(Tok);
900
901 // Not a macro arg identifier?
902 if (!Tok.getIdentifierInfo() ||
903 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
904 Diag(Tok, diag::err_pp_stringize_not_parameter);
905 delete MI;
906
907 // Disable __VA_ARGS__ again.
908 Ident__VA_ARGS__->setIsPoisoned(true);
909 return;
910 }
911
912 // Things look ok, add the param name token to the macro.
913 MI->AddTokenToBody(Tok);
914
915 // Get the next token of the macro.
916 LexUnexpandedToken(Tok);
917 }
918 }
919
920
921 // Disable __VA_ARGS__ again.
922 Ident__VA_ARGS__->setIsPoisoned(true);
923
924 // Check that there is no paste (##) operator at the begining or end of the
925 // replacement list.
926 unsigned NumTokens = MI->getNumTokens();
927 if (NumTokens != 0) {
928 if (MI->getReplacementToken(0).is(tok::hashhash)) {
929 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
930 delete MI;
931 return;
932 }
933 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
934 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
935 delete MI;
936 return;
937 }
938 }
939
940 // If this is the primary source file, remember that this macro hasn't been
941 // used yet.
942 if (isInPrimaryFile())
943 MI->setIsUsed(false);
944
945 // Finally, if this identifier already had a macro defined for it, verify that
946 // the macro bodies are identical and free the old definition.
947 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
948 if (!OtherMI->isUsed())
949 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
950
951 // Macros must be identical. This means all tokes and whitespace separation
952 // must be the same. C99 6.10.3.2.
953 if (!MI->isIdenticalTo(*OtherMI, *this)) {
954 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
955 MacroNameTok.getIdentifierInfo()->getName());
956 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
957 }
958 delete OtherMI;
959 }
960
961 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
962}
963
964/// HandleUndefDirective - Implements #undef.
965///
966void Preprocessor::HandleUndefDirective(Token &UndefTok) {
967 ++NumUndefined;
968
969 Token MacroNameTok;
970 ReadMacroName(MacroNameTok, 2);
971
972 // Error reading macro name? If so, diagnostic already issued.
973 if (MacroNameTok.is(tok::eom))
974 return;
975
976 // Check to see if this is the last token on the #undef line.
977 CheckEndOfDirective("#undef");
978
979 // Okay, we finally have a valid identifier to undef.
980 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
981
982 // If the macro is not defined, this is a noop undef, just return.
983 if (MI == 0) return;
984
985 if (!MI->isUsed())
986 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
987
988 // Free macro definition.
989 delete MI;
990 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
991}
992
993
994//===----------------------------------------------------------------------===//
995// Preprocessor Conditional Directive Handling.
996//===----------------------------------------------------------------------===//
997
998/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
999/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1000/// if any tokens have been returned or pp-directives activated before this
1001/// #ifndef has been lexed.
1002///
1003void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
1004 bool ReadAnyTokensBeforeDirective) {
1005 ++NumIf;
1006 Token DirectiveTok = Result;
1007
1008 Token MacroNameTok;
1009 ReadMacroName(MacroNameTok);
1010
1011 // Error reading macro name? If so, diagnostic already issued.
1012 if (MacroNameTok.is(tok::eom)) {
1013 // Skip code until we get to #endif. This helps with recovery by not
1014 // emitting an error when the #endif is reached.
1015 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1016 /*Foundnonskip*/false, /*FoundElse*/false);
1017 return;
1018 }
1019
1020 // Check to see if this is the last token on the #if[n]def line.
1021 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
1022
1023 if (CurLexer->getConditionalStackDepth() == 0) {
1024 // If the start of a top-level #ifdef, inform MIOpt.
1025 if (!ReadAnyTokensBeforeDirective) {
1026 assert(isIfndef && "#ifdef shouldn't reach here");
1027 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1028 } else
1029 CurLexer->MIOpt.EnterTopLevelConditional();
1030 }
1031
1032 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1033 MacroInfo *MI = getMacroInfo(MII);
1034
1035 // If there is a macro, process it.
1036 if (MI) // Mark it used.
1037 MI->setIsUsed(true);
1038
1039 // Should we include the stuff contained by this directive?
1040 if (!MI == isIfndef) {
1041 // Yes, remember that we are inside a conditional, then lex the next token.
1042 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
1043 /*foundnonskip*/true, /*foundelse*/false);
1044 } else {
1045 // No, skip the contents of this block and return the first token after it.
1046 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1047 /*Foundnonskip*/false,
1048 /*FoundElse*/false);
1049 }
1050}
1051
1052/// HandleIfDirective - Implements the #if directive.
1053///
1054void Preprocessor::HandleIfDirective(Token &IfToken,
1055 bool ReadAnyTokensBeforeDirective) {
1056 ++NumIf;
1057
1058 // Parse and evaluation the conditional expression.
1059 IdentifierInfo *IfNDefMacro = 0;
1060 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
1061
Nuno Lopes0049db62008-06-01 18:31:24 +00001062
1063 // If this condition is equivalent to #ifndef X, and if this is the first
1064 // directive seen, handle it for the multiple-include optimization.
1065 if (CurLexer->getConditionalStackDepth() == 0) {
1066 if (!ReadAnyTokensBeforeDirective && IfNDefMacro)
1067 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
1068 else
1069 CurLexer->MIOpt.EnterTopLevelConditional();
1070 }
1071
Chris Lattner141e71f2008-03-09 01:54:53 +00001072 // Should we include the stuff contained by this directive?
1073 if (ConditionalTrue) {
Chris Lattner141e71f2008-03-09 01:54:53 +00001074 // Yes, remember that we are inside a conditional, then lex the next token.
1075 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
1076 /*foundnonskip*/true, /*foundelse*/false);
1077 } else {
1078 // No, skip the contents of this block and return the first token after it.
1079 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
1080 /*FoundElse*/false);
1081 }
1082}
1083
1084/// HandleEndifDirective - Implements the #endif directive.
1085///
1086void Preprocessor::HandleEndifDirective(Token &EndifToken) {
1087 ++NumEndif;
1088
1089 // Check that this is the whole directive.
1090 CheckEndOfDirective("#endif");
1091
1092 PPConditionalInfo CondInfo;
1093 if (CurLexer->popConditionalLevel(CondInfo)) {
1094 // No conditionals on the stack: this is an #endif without an #if.
1095 return Diag(EndifToken, diag::err_pp_endif_without_if);
1096 }
1097
1098 // If this the end of a top-level #endif, inform MIOpt.
1099 if (CurLexer->getConditionalStackDepth() == 0)
1100 CurLexer->MIOpt.ExitTopLevelConditional();
1101
1102 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
1103 "This code should only be reachable in the non-skipping case!");
1104}
1105
1106
1107void Preprocessor::HandleElseDirective(Token &Result) {
1108 ++NumElse;
1109
1110 // #else directive in a non-skipping conditional... start skipping.
1111 CheckEndOfDirective("#else");
1112
1113 PPConditionalInfo CI;
1114 if (CurLexer->popConditionalLevel(CI))
1115 return Diag(Result, diag::pp_err_else_without_if);
1116
1117 // If this is a top-level #else, inform the MIOpt.
1118 if (CurLexer->getConditionalStackDepth() == 0)
1119 CurLexer->MIOpt.EnterTopLevelConditional();
1120
1121 // If this is a #else with a #else before it, report the error.
1122 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
1123
1124 // Finally, skip the rest of the contents of this block and return the first
1125 // token after it.
1126 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1127 /*FoundElse*/true);
1128}
1129
1130void Preprocessor::HandleElifDirective(Token &ElifToken) {
1131 ++NumElse;
1132
1133 // #elif directive in a non-skipping conditional... start skipping.
1134 // We don't care what the condition is, because we will always skip it (since
1135 // the block immediately before it was included).
1136 DiscardUntilEndOfDirective();
1137
1138 PPConditionalInfo CI;
1139 if (CurLexer->popConditionalLevel(CI))
1140 return Diag(ElifToken, diag::pp_err_elif_without_if);
1141
1142 // If this is a top-level #elif, inform the MIOpt.
1143 if (CurLexer->getConditionalStackDepth() == 0)
1144 CurLexer->MIOpt.EnterTopLevelConditional();
1145
1146 // If this is a #elif with a #else before it, report the error.
1147 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
1148
1149 // Finally, skip the rest of the contents of this block and return the first
1150 // token after it.
1151 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1152 /*FoundElse*/CI.FoundElse);
1153}
1154