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