]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Lex/PPDirectives.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / Lex / PPDirectives.cpp
1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Implements # directive processing for the Preprocessor.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Lex/LiteralSupport.h"
17 #include "clang/Lex/HeaderSearch.h"
18 #include "clang/Lex/MacroInfo.h"
19 #include "clang/Lex/LexDiagnostic.h"
20 #include "clang/Lex/CodeCompletionHandler.h"
21 #include "clang/Lex/ModuleLoader.h"
22 #include "clang/Lex/Pragma.h"
23 #include "clang/Basic/FileManager.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/Support/ErrorHandling.h"
27 using namespace clang;
28
29 //===----------------------------------------------------------------------===//
30 // Utility Methods for Preprocessor Directive Handling.
31 //===----------------------------------------------------------------------===//
32
33 MacroInfo *Preprocessor::AllocateMacroInfo() {
34   MacroInfoChain *MIChain;
35
36   if (MICache) {
37     MIChain = MICache;
38     MICache = MICache->Next;
39   }
40   else {
41     MIChain = BP.Allocate<MacroInfoChain>();
42   }
43
44   MIChain->Next = MIChainHead;
45   MIChain->Prev = 0;
46   if (MIChainHead)
47     MIChainHead->Prev = MIChain;
48   MIChainHead = MIChain;
49
50   return &(MIChain->MI);
51 }
52
53 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
54   MacroInfo *MI = AllocateMacroInfo();
55   new (MI) MacroInfo(L);
56   return MI;
57 }
58
59 MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) {
60   MacroInfo *MI = AllocateMacroInfo();
61   new (MI) MacroInfo(MacroToClone, BP);
62   return MI;
63 }
64
65 /// \brief Release the specified MacroInfo to be reused for allocating
66 /// new MacroInfo objects.
67 void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) {
68   MacroInfoChain *MIChain = (MacroInfoChain*) MI;
69   if (MacroInfoChain *Prev = MIChain->Prev) {
70     MacroInfoChain *Next = MIChain->Next;
71     Prev->Next = Next;
72     if (Next)
73       Next->Prev = Prev;
74   }
75   else {
76     assert(MIChainHead == MIChain);
77     MIChainHead = MIChain->Next;
78     MIChainHead->Prev = 0;
79   }
80   MIChain->Next = MICache;
81   MICache = MIChain;
82
83   MI->Destroy();
84 }
85
86 /// \brief Read and discard all tokens remaining on the current line until
87 /// the tok::eod token is found.
88 void Preprocessor::DiscardUntilEndOfDirective() {
89   Token Tmp;
90   do {
91     LexUnexpandedToken(Tmp);
92     assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
93   } while (Tmp.isNot(tok::eod));
94 }
95
96 /// \brief Lex and validate a macro name, which occurs after a
97 /// \#define or \#undef.
98 ///
99 /// This sets the token kind to eod and discards the rest
100 /// of the macro line if the macro name is invalid.  \p isDefineUndef is 1 if
101 /// this is due to a a \#define, 2 if \#undef directive, 0 if it is something
102 /// else (e.g. \#ifdef).
103 void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
104   // Read the token, don't allow macro expansion on it.
105   LexUnexpandedToken(MacroNameTok);
106
107   if (MacroNameTok.is(tok::code_completion)) {
108     if (CodeComplete)
109       CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
110     setCodeCompletionReached();
111     LexUnexpandedToken(MacroNameTok);
112   }
113   
114   // Missing macro name?
115   if (MacroNameTok.is(tok::eod)) {
116     Diag(MacroNameTok, diag::err_pp_missing_macro_name);
117     return;
118   }
119
120   IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
121   if (II == 0) {
122     bool Invalid = false;
123     std::string Spelling = getSpelling(MacroNameTok, &Invalid);
124     if (Invalid)
125       return;
126
127     const IdentifierInfo &Info = Identifiers.get(Spelling);
128
129     // Allow #defining |and| and friends in microsoft mode.
130     if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MicrosoftMode) {
131       MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling));
132       return;
133     }
134
135     if (Info.isCPlusPlusOperatorKeyword())
136       // C++ 2.5p2: Alternative tokens behave the same as its primary token
137       // except for their spellings.
138       Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling;
139     else
140       Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
141     // Fall through on error.
142   } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
143     // Error if defining "defined": C99 6.10.8.4.
144     Diag(MacroNameTok, diag::err_defined_macro_name);
145   } else if (isDefineUndef && II->hasMacroDefinition() &&
146              getMacroInfo(II)->isBuiltinMacro()) {
147     // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
148     if (isDefineUndef == 1)
149       Diag(MacroNameTok, diag::pp_redef_builtin_macro);
150     else
151       Diag(MacroNameTok, diag::pp_undef_builtin_macro);
152   } else {
153     // Okay, we got a good identifier node.  Return it.
154     return;
155   }
156
157   // Invalid macro name, read and discard the rest of the line.  Then set the
158   // token kind to tok::eod.
159   MacroNameTok.setKind(tok::eod);
160   return DiscardUntilEndOfDirective();
161 }
162
163 /// \brief Ensure that the next token is a tok::eod token.
164 ///
165 /// If not, emit a diagnostic and consume up until the eod.  If EnableMacros is
166 /// true, then we consider macros that expand to zero tokens as being ok.
167 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
168   Token Tmp;
169   // Lex unexpanded tokens for most directives: macros might expand to zero
170   // tokens, causing us to miss diagnosing invalid lines.  Some directives (like
171   // #line) allow empty macros.
172   if (EnableMacros)
173     Lex(Tmp);
174   else
175     LexUnexpandedToken(Tmp);
176
177   // There should be no tokens after the directive, but we allow them as an
178   // extension.
179   while (Tmp.is(tok::comment))  // Skip comments in -C mode.
180     LexUnexpandedToken(Tmp);
181
182   if (Tmp.isNot(tok::eod)) {
183     // Add a fixit in GNU/C99/C++ mode.  Don't offer a fixit for strict-C89,
184     // or if this is a macro-style preprocessing directive, because it is more
185     // trouble than it is worth to insert /**/ and check that there is no /**/
186     // in the range also.
187     FixItHint Hint;
188     if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
189         !CurTokenLexer)
190       Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
191     Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
192     DiscardUntilEndOfDirective();
193   }
194 }
195
196
197
198 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
199 /// decided that the subsequent tokens are in the \#if'd out portion of the
200 /// file.  Lex the rest of the file, until we see an \#endif.  If
201 /// FoundNonSkipPortion is true, then we have already emitted code for part of
202 /// this \#if directive, so \#else/\#elif blocks should never be entered.
203 /// If ElseOk is true, then \#else directives are ok, if not, then we have
204 /// already seen one so a \#else directive is a duplicate.  When this returns,
205 /// the caller can lex the first valid token.
206 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
207                                                 bool FoundNonSkipPortion,
208                                                 bool FoundElse,
209                                                 SourceLocation ElseLoc) {
210   ++NumSkipped;
211   assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?");
212
213   CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
214                                  FoundNonSkipPortion, FoundElse);
215
216   if (CurPTHLexer) {
217     PTHSkipExcludedConditionalBlock();
218     return;
219   }
220
221   // Enter raw mode to disable identifier lookup (and thus macro expansion),
222   // disabling warnings, etc.
223   CurPPLexer->LexingRawMode = true;
224   Token Tok;
225   while (1) {
226     CurLexer->Lex(Tok);
227
228     if (Tok.is(tok::code_completion)) {
229       if (CodeComplete)
230         CodeComplete->CodeCompleteInConditionalExclusion();
231       setCodeCompletionReached();
232       continue;
233     }
234     
235     // If this is the end of the buffer, we have an error.
236     if (Tok.is(tok::eof)) {
237       // Emit errors for each unterminated conditional on the stack, including
238       // the current one.
239       while (!CurPPLexer->ConditionalStack.empty()) {
240         if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
241           Diag(CurPPLexer->ConditionalStack.back().IfLoc,
242                diag::err_pp_unterminated_conditional);
243         CurPPLexer->ConditionalStack.pop_back();
244       }
245
246       // Just return and let the caller lex after this #include.
247       break;
248     }
249
250     // If this token is not a preprocessor directive, just skip it.
251     if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
252       continue;
253
254     // We just parsed a # character at the start of a line, so we're in
255     // directive mode.  Tell the lexer this so any newlines we see will be
256     // converted into an EOD token (this terminates the macro).
257     CurPPLexer->ParsingPreprocessorDirective = true;
258     if (CurLexer) CurLexer->SetCommentRetentionState(false);
259
260
261     // Read the next token, the directive flavor.
262     LexUnexpandedToken(Tok);
263
264     // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
265     // something bogus), skip it.
266     if (Tok.isNot(tok::raw_identifier)) {
267       CurPPLexer->ParsingPreprocessorDirective = false;
268       // Restore comment saving mode.
269       if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
270       continue;
271     }
272
273     // If the first letter isn't i or e, it isn't intesting to us.  We know that
274     // this is safe in the face of spelling differences, because there is no way
275     // to spell an i/e in a strange way that is another letter.  Skipping this
276     // allows us to avoid looking up the identifier info for #define/#undef and
277     // other common directives.
278     const char *RawCharData = Tok.getRawIdentifierData();
279
280     char FirstChar = RawCharData[0];
281     if (FirstChar >= 'a' && FirstChar <= 'z' &&
282         FirstChar != 'i' && FirstChar != 'e') {
283       CurPPLexer->ParsingPreprocessorDirective = false;
284       // Restore comment saving mode.
285       if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
286       continue;
287     }
288
289     // Get the identifier name without trigraphs or embedded newlines.  Note
290     // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
291     // when skipping.
292     char DirectiveBuf[20];
293     StringRef Directive;
294     if (!Tok.needsCleaning() && Tok.getLength() < 20) {
295       Directive = StringRef(RawCharData, Tok.getLength());
296     } else {
297       std::string DirectiveStr = getSpelling(Tok);
298       unsigned IdLen = DirectiveStr.size();
299       if (IdLen >= 20) {
300         CurPPLexer->ParsingPreprocessorDirective = false;
301         // Restore comment saving mode.
302         if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
303         continue;
304       }
305       memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
306       Directive = StringRef(DirectiveBuf, IdLen);
307     }
308
309     if (Directive.startswith("if")) {
310       StringRef Sub = Directive.substr(2);
311       if (Sub.empty() ||   // "if"
312           Sub == "def" ||   // "ifdef"
313           Sub == "ndef") {  // "ifndef"
314         // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
315         // bother parsing the condition.
316         DiscardUntilEndOfDirective();
317         CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
318                                        /*foundnonskip*/false,
319                                        /*foundelse*/false);
320       }
321     } else if (Directive[0] == 'e') {
322       StringRef Sub = Directive.substr(1);
323       if (Sub == "ndif") {  // "endif"
324         PPConditionalInfo CondInfo;
325         CondInfo.WasSkipping = true; // Silence bogus warning.
326         bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
327         (void)InCond;  // Silence warning in no-asserts mode.
328         assert(!InCond && "Can't be skipping if not in a conditional!");
329
330         // If we popped the outermost skipping block, we're done skipping!
331         if (!CondInfo.WasSkipping) {
332           // Restore the value of LexingRawMode so that trailing comments
333           // are handled correctly, if we've reached the outermost block.
334           CurPPLexer->LexingRawMode = false;
335           CheckEndOfDirective("endif");
336           CurPPLexer->LexingRawMode = true;
337           if (Callbacks)
338             Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
339           break;
340         } else {
341           DiscardUntilEndOfDirective();
342         }
343       } else if (Sub == "lse") { // "else".
344         // #else directive in a skipping conditional.  If not in some other
345         // skipping conditional, and if #else hasn't already been seen, enter it
346         // as a non-skipping conditional.
347         PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
348
349         // If this is a #else with a #else before it, report the error.
350         if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
351
352         // Note that we've seen a #else in this conditional.
353         CondInfo.FoundElse = true;
354
355         // If the conditional is at the top level, and the #if block wasn't
356         // entered, enter the #else block now.
357         if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
358           CondInfo.FoundNonSkip = true;
359           // Restore the value of LexingRawMode so that trailing comments
360           // are handled correctly.
361           CurPPLexer->LexingRawMode = false;
362           CheckEndOfDirective("else");
363           CurPPLexer->LexingRawMode = true;
364           if (Callbacks)
365             Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
366           break;
367         } else {
368           DiscardUntilEndOfDirective();  // C99 6.10p4.
369         }
370       } else if (Sub == "lif") {  // "elif".
371         PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
372
373         bool ShouldEnter;
374         const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
375         // If this is in a skipping block or if we're already handled this #if
376         // block, don't bother parsing the condition.
377         if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
378           DiscardUntilEndOfDirective();
379           ShouldEnter = false;
380         } else {
381           // Restore the value of LexingRawMode so that identifiers are
382           // looked up, etc, inside the #elif expression.
383           assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
384           CurPPLexer->LexingRawMode = false;
385           IdentifierInfo *IfNDefMacro = 0;
386           ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
387           CurPPLexer->LexingRawMode = true;
388         }
389         const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
390
391         // If this is a #elif with a #else before it, report the error.
392         if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
393
394         // If this condition is true, enter it!
395         if (ShouldEnter) {
396           CondInfo.FoundNonSkip = true;
397           if (Callbacks)
398             Callbacks->Elif(Tok.getLocation(),
399                             SourceRange(ConditionalBegin, ConditionalEnd),
400                             CondInfo.IfLoc);
401           break;
402         }
403       }
404     }
405
406     CurPPLexer->ParsingPreprocessorDirective = false;
407     // Restore comment saving mode.
408     if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
409   }
410
411   // Finally, if we are out of the conditional (saw an #endif or ran off the end
412   // of the file, just stop skipping and return to lexing whatever came after
413   // the #if block.
414   CurPPLexer->LexingRawMode = false;
415
416   if (Callbacks) {
417     SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
418     Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
419   }
420 }
421
422 void Preprocessor::PTHSkipExcludedConditionalBlock() {
423
424   while (1) {
425     assert(CurPTHLexer);
426     assert(CurPTHLexer->LexingRawMode == false);
427
428     // Skip to the next '#else', '#elif', or #endif.
429     if (CurPTHLexer->SkipBlock()) {
430       // We have reached an #endif.  Both the '#' and 'endif' tokens
431       // have been consumed by the PTHLexer.  Just pop off the condition level.
432       PPConditionalInfo CondInfo;
433       bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
434       (void)InCond;  // Silence warning in no-asserts mode.
435       assert(!InCond && "Can't be skipping if not in a conditional!");
436       break;
437     }
438
439     // We have reached a '#else' or '#elif'.  Lex the next token to get
440     // the directive flavor.
441     Token Tok;
442     LexUnexpandedToken(Tok);
443
444     // We can actually look up the IdentifierInfo here since we aren't in
445     // raw mode.
446     tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
447
448     if (K == tok::pp_else) {
449       // #else: Enter the else condition.  We aren't in a nested condition
450       //  since we skip those. We're always in the one matching the last
451       //  blocked we skipped.
452       PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
453       // Note that we've seen a #else in this conditional.
454       CondInfo.FoundElse = true;
455
456       // If the #if block wasn't entered then enter the #else block now.
457       if (!CondInfo.FoundNonSkip) {
458         CondInfo.FoundNonSkip = true;
459
460         // Scan until the eod token.
461         CurPTHLexer->ParsingPreprocessorDirective = true;
462         DiscardUntilEndOfDirective();
463         CurPTHLexer->ParsingPreprocessorDirective = false;
464
465         break;
466       }
467
468       // Otherwise skip this block.
469       continue;
470     }
471
472     assert(K == tok::pp_elif);
473     PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
474
475     // If this is a #elif with a #else before it, report the error.
476     if (CondInfo.FoundElse)
477       Diag(Tok, diag::pp_err_elif_after_else);
478
479     // If this is in a skipping block or if we're already handled this #if
480     // block, don't bother parsing the condition.  We just skip this block.
481     if (CondInfo.FoundNonSkip)
482       continue;
483
484     // Evaluate the condition of the #elif.
485     IdentifierInfo *IfNDefMacro = 0;
486     CurPTHLexer->ParsingPreprocessorDirective = true;
487     bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
488     CurPTHLexer->ParsingPreprocessorDirective = false;
489
490     // If this condition is true, enter it!
491     if (ShouldEnter) {
492       CondInfo.FoundNonSkip = true;
493       break;
494     }
495
496     // Otherwise, skip this block and go to the next one.
497     continue;
498   }
499 }
500
501 const FileEntry *Preprocessor::LookupFile(
502     StringRef Filename,
503     bool isAngled,
504     const DirectoryLookup *FromDir,
505     const DirectoryLookup *&CurDir,
506     SmallVectorImpl<char> *SearchPath,
507     SmallVectorImpl<char> *RelativePath,
508     Module **SuggestedModule,
509     bool SkipCache) {
510   // If the header lookup mechanism may be relative to the current file, pass in
511   // info about where the current file is.
512   const FileEntry *CurFileEnt = 0;
513   if (!FromDir) {
514     FileID FID = getCurrentFileLexer()->getFileID();
515     CurFileEnt = SourceMgr.getFileEntryForID(FID);
516
517     // If there is no file entry associated with this file, it must be the
518     // predefines buffer.  Any other file is not lexed with a normal lexer, so
519     // it won't be scanned for preprocessor directives.   If we have the
520     // predefines buffer, resolve #include references (which come from the
521     // -include command line argument) as if they came from the main file, this
522     // affects file lookup etc.
523     if (CurFileEnt == 0) {
524       FID = SourceMgr.getMainFileID();
525       CurFileEnt = SourceMgr.getFileEntryForID(FID);
526     }
527   }
528
529   // Do a standard file entry lookup.
530   CurDir = CurDirLookup;
531   const FileEntry *FE = HeaderInfo.LookupFile(
532       Filename, isAngled, FromDir, CurDir, CurFileEnt,
533       SearchPath, RelativePath, SuggestedModule, SkipCache);
534   if (FE) return FE;
535
536   // Otherwise, see if this is a subframework header.  If so, this is relative
537   // to one of the headers on the #include stack.  Walk the list of the current
538   // headers on the #include stack and pass them to HeaderInfo.
539   // FIXME: SuggestedModule!
540   if (IsFileLexer()) {
541     if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
542       if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
543                                                     SearchPath, RelativePath)))
544         return FE;
545   }
546
547   for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
548     IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
549     if (IsFileLexer(ISEntry)) {
550       if ((CurFileEnt =
551            SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
552         if ((FE = HeaderInfo.LookupSubframeworkHeader(
553                 Filename, CurFileEnt, SearchPath, RelativePath)))
554           return FE;
555     }
556   }
557
558   // Otherwise, we really couldn't find the file.
559   return 0;
560 }
561
562
563 //===----------------------------------------------------------------------===//
564 // Preprocessor Directive Handling.
565 //===----------------------------------------------------------------------===//
566
567 class Preprocessor::ResetMacroExpansionHelper {
568 public:
569   ResetMacroExpansionHelper(Preprocessor *pp)
570     : PP(pp), save(pp->DisableMacroExpansion) {
571     if (pp->MacroExpansionInDirectivesOverride)
572       pp->DisableMacroExpansion = false;
573   }
574   ~ResetMacroExpansionHelper() {
575     PP->DisableMacroExpansion = save;
576   }
577 private:
578   Preprocessor *PP;
579   bool save;
580 };
581
582 /// HandleDirective - This callback is invoked when the lexer sees a # token
583 /// at the start of a line.  This consumes the directive, modifies the
584 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
585 /// read is the correct one.
586 void Preprocessor::HandleDirective(Token &Result) {
587   // FIXME: Traditional: # with whitespace before it not recognized by K&R?
588
589   // We just parsed a # character at the start of a line, so we're in directive
590   // mode.  Tell the lexer this so any newlines we see will be converted into an
591   // EOD token (which terminates the directive).
592   CurPPLexer->ParsingPreprocessorDirective = true;
593
594   ++NumDirectives;
595
596   // We are about to read a token.  For the multiple-include optimization FA to
597   // work, we have to remember if we had read any tokens *before* this
598   // pp-directive.
599   bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
600
601   // Save the '#' token in case we need to return it later.
602   Token SavedHash = Result;
603
604   // Read the next token, the directive flavor.  This isn't expanded due to
605   // C99 6.10.3p8.
606   LexUnexpandedToken(Result);
607
608   // C99 6.10.3p11: Is this preprocessor directive in macro invocation?  e.g.:
609   //   #define A(x) #x
610   //   A(abc
611   //     #warning blah
612   //   def)
613   // If so, the user is relying on undefined behavior, emit a diagnostic. Do
614   // not support this for #include-like directives, since that can result in
615   // terrible diagnostics, and does not work in GCC.
616   if (InMacroArgs) {
617     if (IdentifierInfo *II = Result.getIdentifierInfo()) {
618       switch (II->getPPKeywordID()) {
619       case tok::pp_include:
620       case tok::pp_import:
621       case tok::pp_include_next:
622       case tok::pp___include_macros:
623         Diag(Result, diag::err_embedded_include) << II->getName();
624         DiscardUntilEndOfDirective();
625         return;
626       default:
627         break;
628       }
629     }
630     Diag(Result, diag::ext_embedded_directive);
631   }
632
633   // Temporarily enable macro expansion if set so
634   // and reset to previous state when returning from this function.
635   ResetMacroExpansionHelper helper(this);
636
637 TryAgain:
638   switch (Result.getKind()) {
639   case tok::eod:
640     return;   // null directive.
641   case tok::comment:
642     // Handle stuff like "# /*foo*/ define X" in -E -C mode.
643     LexUnexpandedToken(Result);
644     goto TryAgain;
645   case tok::code_completion:
646     if (CodeComplete)
647       CodeComplete->CodeCompleteDirective(
648                                     CurPPLexer->getConditionalStackDepth() > 0);
649     setCodeCompletionReached();
650     return;
651   case tok::numeric_constant:  // # 7  GNU line marker directive.
652     if (getLangOpts().AsmPreprocessor)
653       break;  // # 4 is not a preprocessor directive in .S files.
654     return HandleDigitDirective(Result);
655   default:
656     IdentifierInfo *II = Result.getIdentifierInfo();
657     if (II == 0) break;  // Not an identifier.
658
659     // Ask what the preprocessor keyword ID is.
660     switch (II->getPPKeywordID()) {
661     default: break;
662     // C99 6.10.1 - Conditional Inclusion.
663     case tok::pp_if:
664       return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
665     case tok::pp_ifdef:
666       return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
667     case tok::pp_ifndef:
668       return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
669     case tok::pp_elif:
670       return HandleElifDirective(Result);
671     case tok::pp_else:
672       return HandleElseDirective(Result);
673     case tok::pp_endif:
674       return HandleEndifDirective(Result);
675
676     // C99 6.10.2 - Source File Inclusion.
677     case tok::pp_include:
678       // Handle #include.
679       return HandleIncludeDirective(SavedHash.getLocation(), Result);
680     case tok::pp___include_macros:
681       // Handle -imacros.
682       return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); 
683
684     // C99 6.10.3 - Macro Replacement.
685     case tok::pp_define:
686       return HandleDefineDirective(Result);
687     case tok::pp_undef:
688       return HandleUndefDirective(Result);
689
690     // C99 6.10.4 - Line Control.
691     case tok::pp_line:
692       return HandleLineDirective(Result);
693
694     // C99 6.10.5 - Error Directive.
695     case tok::pp_error:
696       return HandleUserDiagnosticDirective(Result, false);
697
698     // C99 6.10.6 - Pragma Directive.
699     case tok::pp_pragma:
700       return HandlePragmaDirective(PIK_HashPragma);
701
702     // GNU Extensions.
703     case tok::pp_import:
704       return HandleImportDirective(SavedHash.getLocation(), Result);
705     case tok::pp_include_next:
706       return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
707
708     case tok::pp_warning:
709       Diag(Result, diag::ext_pp_warning_directive);
710       return HandleUserDiagnosticDirective(Result, true);
711     case tok::pp_ident:
712       return HandleIdentSCCSDirective(Result);
713     case tok::pp_sccs:
714       return HandleIdentSCCSDirective(Result);
715     case tok::pp_assert:
716       //isExtension = true;  // FIXME: implement #assert
717       break;
718     case tok::pp_unassert:
719       //isExtension = true;  // FIXME: implement #unassert
720       break;
721         
722     case tok::pp___public_macro:
723       if (getLangOpts().Modules)
724         return HandleMacroPublicDirective(Result);
725       break;
726         
727     case tok::pp___private_macro:
728       if (getLangOpts().Modules)
729         return HandleMacroPrivateDirective(Result);
730       break;
731     }
732     break;
733   }
734
735   // If this is a .S file, treat unknown # directives as non-preprocessor
736   // directives.  This is important because # may be a comment or introduce
737   // various pseudo-ops.  Just return the # token and push back the following
738   // token to be lexed next time.
739   if (getLangOpts().AsmPreprocessor) {
740     Token *Toks = new Token[2];
741     // Return the # and the token after it.
742     Toks[0] = SavedHash;
743     Toks[1] = Result;
744     
745     // If the second token is a hashhash token, then we need to translate it to
746     // unknown so the token lexer doesn't try to perform token pasting.
747     if (Result.is(tok::hashhash))
748       Toks[1].setKind(tok::unknown);
749     
750     // Enter this token stream so that we re-lex the tokens.  Make sure to
751     // enable macro expansion, in case the token after the # is an identifier
752     // that is expanded.
753     EnterTokenStream(Toks, 2, false, true);
754     return;
755   }
756
757   // If we reached here, the preprocessing token is not valid!
758   Diag(Result, diag::err_pp_invalid_directive);
759
760   // Read the rest of the PP line.
761   DiscardUntilEndOfDirective();
762
763   // Okay, we're done parsing the directive.
764 }
765
766 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
767 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
768 static bool GetLineValue(Token &DigitTok, unsigned &Val,
769                          unsigned DiagID, Preprocessor &PP) {
770   if (DigitTok.isNot(tok::numeric_constant)) {
771     PP.Diag(DigitTok, DiagID);
772
773     if (DigitTok.isNot(tok::eod))
774       PP.DiscardUntilEndOfDirective();
775     return true;
776   }
777
778   SmallString<64> IntegerBuffer;
779   IntegerBuffer.resize(DigitTok.getLength());
780   const char *DigitTokBegin = &IntegerBuffer[0];
781   bool Invalid = false;
782   unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
783   if (Invalid)
784     return true;
785   
786   // Verify that we have a simple digit-sequence, and compute the value.  This
787   // is always a simple digit string computed in decimal, so we do this manually
788   // here.
789   Val = 0;
790   for (unsigned i = 0; i != ActualLength; ++i) {
791     if (!isdigit(DigitTokBegin[i])) {
792       PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
793               diag::err_pp_line_digit_sequence);
794       PP.DiscardUntilEndOfDirective();
795       return true;
796     }
797
798     unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
799     if (NextVal < Val) { // overflow.
800       PP.Diag(DigitTok, DiagID);
801       PP.DiscardUntilEndOfDirective();
802       return true;
803     }
804     Val = NextVal;
805   }
806
807   if (DigitTokBegin[0] == '0' && Val)
808     PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal);
809
810   return false;
811 }
812
813 /// \brief Handle a \#line directive: C99 6.10.4.
814 ///
815 /// The two acceptable forms are:
816 /// \verbatim
817 ///   # line digit-sequence
818 ///   # line digit-sequence "s-char-sequence"
819 /// \endverbatim
820 void Preprocessor::HandleLineDirective(Token &Tok) {
821   // Read the line # and string argument.  Per C99 6.10.4p5, these tokens are
822   // expanded.
823   Token DigitTok;
824   Lex(DigitTok);
825
826   // Validate the number and convert it to an unsigned.
827   unsigned LineNo;
828   if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
829     return;
830   
831   if (LineNo == 0)
832     Diag(DigitTok, diag::ext_pp_line_zero);
833
834   // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
835   // number greater than 2147483647".  C90 requires that the line # be <= 32767.
836   unsigned LineLimit = 32768U;
837   if (LangOpts.C99 || LangOpts.CPlusPlus0x)
838     LineLimit = 2147483648U;
839   if (LineNo >= LineLimit)
840     Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
841   else if (LangOpts.CPlusPlus0x && LineNo >= 32768U)
842     Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
843
844   int FilenameID = -1;
845   Token StrTok;
846   Lex(StrTok);
847
848   // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
849   // string followed by eod.
850   if (StrTok.is(tok::eod))
851     ; // ok
852   else if (StrTok.isNot(tok::string_literal)) {
853     Diag(StrTok, diag::err_pp_line_invalid_filename);
854     return DiscardUntilEndOfDirective();
855   } else if (StrTok.hasUDSuffix()) {
856     Diag(StrTok, diag::err_invalid_string_udl);
857     return DiscardUntilEndOfDirective();
858   } else {
859     // Parse and validate the string, converting it into a unique ID.
860     StringLiteralParser Literal(&StrTok, 1, *this);
861     assert(Literal.isAscii() && "Didn't allow wide strings in");
862     if (Literal.hadError)
863       return DiscardUntilEndOfDirective();
864     if (Literal.Pascal) {
865       Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
866       return DiscardUntilEndOfDirective();
867     }
868     FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
869
870     // Verify that there is nothing after the string, other than EOD.  Because
871     // of C99 6.10.4p5, macros that expand to empty tokens are ok.
872     CheckEndOfDirective("line", true);
873   }
874
875   SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
876
877   if (Callbacks)
878     Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
879                            PPCallbacks::RenameFile,
880                            SrcMgr::C_User);
881 }
882
883 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
884 /// marker directive.
885 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
886                                 bool &IsSystemHeader, bool &IsExternCHeader,
887                                 Preprocessor &PP) {
888   unsigned FlagVal;
889   Token FlagTok;
890   PP.Lex(FlagTok);
891   if (FlagTok.is(tok::eod)) return false;
892   if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
893     return true;
894
895   if (FlagVal == 1) {
896     IsFileEntry = true;
897
898     PP.Lex(FlagTok);
899     if (FlagTok.is(tok::eod)) return false;
900     if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
901       return true;
902   } else if (FlagVal == 2) {
903     IsFileExit = true;
904
905     SourceManager &SM = PP.getSourceManager();
906     // If we are leaving the current presumed file, check to make sure the
907     // presumed include stack isn't empty!
908     FileID CurFileID =
909       SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
910     PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
911     if (PLoc.isInvalid())
912       return true;
913     
914     // If there is no include loc (main file) or if the include loc is in a
915     // different physical file, then we aren't in a "1" line marker flag region.
916     SourceLocation IncLoc = PLoc.getIncludeLoc();
917     if (IncLoc.isInvalid() ||
918         SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
919       PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
920       PP.DiscardUntilEndOfDirective();
921       return true;
922     }
923
924     PP.Lex(FlagTok);
925     if (FlagTok.is(tok::eod)) return false;
926     if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
927       return true;
928   }
929
930   // We must have 3 if there are still flags.
931   if (FlagVal != 3) {
932     PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
933     PP.DiscardUntilEndOfDirective();
934     return true;
935   }
936
937   IsSystemHeader = true;
938
939   PP.Lex(FlagTok);
940   if (FlagTok.is(tok::eod)) return false;
941   if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
942     return true;
943
944   // We must have 4 if there is yet another flag.
945   if (FlagVal != 4) {
946     PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
947     PP.DiscardUntilEndOfDirective();
948     return true;
949   }
950
951   IsExternCHeader = true;
952
953   PP.Lex(FlagTok);
954   if (FlagTok.is(tok::eod)) return false;
955
956   // There are no more valid flags here.
957   PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
958   PP.DiscardUntilEndOfDirective();
959   return true;
960 }
961
962 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
963 /// one of the following forms:
964 ///
965 ///     # 42
966 ///     # 42 "file" ('1' | '2')?
967 ///     # 42 "file" ('1' | '2')? '3' '4'?
968 ///
969 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
970   // Validate the number and convert it to an unsigned.  GNU does not have a
971   // line # limit other than it fit in 32-bits.
972   unsigned LineNo;
973   if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
974                    *this))
975     return;
976
977   Token StrTok;
978   Lex(StrTok);
979
980   bool IsFileEntry = false, IsFileExit = false;
981   bool IsSystemHeader = false, IsExternCHeader = false;
982   int FilenameID = -1;
983
984   // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
985   // string followed by eod.
986   if (StrTok.is(tok::eod))
987     ; // ok
988   else if (StrTok.isNot(tok::string_literal)) {
989     Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
990     return DiscardUntilEndOfDirective();
991   } else if (StrTok.hasUDSuffix()) {
992     Diag(StrTok, diag::err_invalid_string_udl);
993     return DiscardUntilEndOfDirective();
994   } else {
995     // Parse and validate the string, converting it into a unique ID.
996     StringLiteralParser Literal(&StrTok, 1, *this);
997     assert(Literal.isAscii() && "Didn't allow wide strings in");
998     if (Literal.hadError)
999       return DiscardUntilEndOfDirective();
1000     if (Literal.Pascal) {
1001       Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1002       return DiscardUntilEndOfDirective();
1003     }
1004     FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1005
1006     // If a filename was present, read any flags that are present.
1007     if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1008                             IsSystemHeader, IsExternCHeader, *this))
1009       return;
1010   }
1011
1012   // Create a line note with this information.
1013   SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1014                         IsFileEntry, IsFileExit,
1015                         IsSystemHeader, IsExternCHeader);
1016
1017   // If the preprocessor has callbacks installed, notify them of the #line
1018   // change.  This is used so that the line marker comes out in -E mode for
1019   // example.
1020   if (Callbacks) {
1021     PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1022     if (IsFileEntry)
1023       Reason = PPCallbacks::EnterFile;
1024     else if (IsFileExit)
1025       Reason = PPCallbacks::ExitFile;
1026     SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1027     if (IsExternCHeader)
1028       FileKind = SrcMgr::C_ExternCSystem;
1029     else if (IsSystemHeader)
1030       FileKind = SrcMgr::C_System;
1031
1032     Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1033   }
1034 }
1035
1036
1037 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1038 ///
1039 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1040                                                  bool isWarning) {
1041   // PTH doesn't emit #warning or #error directives.
1042   if (CurPTHLexer)
1043     return CurPTHLexer->DiscardToEndOfLine();
1044
1045   // Read the rest of the line raw.  We do this because we don't want macros
1046   // to be expanded and we don't require that the tokens be valid preprocessing
1047   // tokens.  For example, this is allowed: "#warning `   'foo".  GCC does
1048   // collapse multiple consequtive white space between tokens, but this isn't
1049   // specified by the standard.
1050   SmallString<128> Message;
1051   CurLexer->ReadToEndOfLine(&Message);
1052
1053   // Find the first non-whitespace character, so that we can make the
1054   // diagnostic more succinct.
1055   StringRef Msg = Message.str().ltrim(" ");
1056
1057   if (isWarning)
1058     Diag(Tok, diag::pp_hash_warning) << Msg;
1059   else
1060     Diag(Tok, diag::err_pp_hash_error) << Msg;
1061 }
1062
1063 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1064 ///
1065 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1066   // Yes, this directive is an extension.
1067   Diag(Tok, diag::ext_pp_ident_directive);
1068
1069   // Read the string argument.
1070   Token StrTok;
1071   Lex(StrTok);
1072
1073   // If the token kind isn't a string, it's a malformed directive.
1074   if (StrTok.isNot(tok::string_literal) &&
1075       StrTok.isNot(tok::wide_string_literal)) {
1076     Diag(StrTok, diag::err_pp_malformed_ident);
1077     if (StrTok.isNot(tok::eod))
1078       DiscardUntilEndOfDirective();
1079     return;
1080   }
1081
1082   if (StrTok.hasUDSuffix()) {
1083     Diag(StrTok, diag::err_invalid_string_udl);
1084     return DiscardUntilEndOfDirective();
1085   }
1086
1087   // Verify that there is nothing after the string, other than EOD.
1088   CheckEndOfDirective("ident");
1089
1090   if (Callbacks) {
1091     bool Invalid = false;
1092     std::string Str = getSpelling(StrTok, &Invalid);
1093     if (!Invalid)
1094       Callbacks->Ident(Tok.getLocation(), Str);
1095   }
1096 }
1097
1098 /// \brief Handle a #public directive.
1099 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1100   Token MacroNameTok;
1101   ReadMacroName(MacroNameTok, 2);
1102   
1103   // Error reading macro name?  If so, diagnostic already issued.
1104   if (MacroNameTok.is(tok::eod))
1105     return;
1106
1107   // Check to see if this is the last token on the #__public_macro line.
1108   CheckEndOfDirective("__public_macro");
1109
1110   // Okay, we finally have a valid identifier to undef.
1111   MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
1112   
1113   // If the macro is not defined, this is an error.
1114   if (MI == 0) {
1115     Diag(MacroNameTok, diag::err_pp_visibility_non_macro)
1116       << MacroNameTok.getIdentifierInfo();
1117     return;
1118   }
1119   
1120   // Note that this macro has now been exported.
1121   MI->setVisibility(/*IsPublic=*/true, MacroNameTok.getLocation());
1122   
1123   // If this macro definition came from a PCH file, mark it
1124   // as having changed since serialization.
1125   if (MI->isFromAST())
1126     MI->setChangedAfterLoad();
1127 }
1128
1129 /// \brief Handle a #private directive.
1130 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1131   Token MacroNameTok;
1132   ReadMacroName(MacroNameTok, 2);
1133   
1134   // Error reading macro name?  If so, diagnostic already issued.
1135   if (MacroNameTok.is(tok::eod))
1136     return;
1137   
1138   // Check to see if this is the last token on the #__private_macro line.
1139   CheckEndOfDirective("__private_macro");
1140   
1141   // Okay, we finally have a valid identifier to undef.
1142   MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
1143   
1144   // If the macro is not defined, this is an error.
1145   if (MI == 0) {
1146     Diag(MacroNameTok, diag::err_pp_visibility_non_macro)
1147       << MacroNameTok.getIdentifierInfo();
1148     return;
1149   }
1150   
1151   // Note that this macro has now been marked private.
1152   MI->setVisibility(/*IsPublic=*/false, MacroNameTok.getLocation());
1153   
1154   // If this macro definition came from a PCH file, mark it
1155   // as having changed since serialization.
1156   if (MI->isFromAST())
1157     MI->setChangedAfterLoad();
1158 }
1159
1160 //===----------------------------------------------------------------------===//
1161 // Preprocessor Include Directive Handling.
1162 //===----------------------------------------------------------------------===//
1163
1164 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1165 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1166 /// true if the input filename was in <>'s or false if it were in ""'s.  The
1167 /// caller is expected to provide a buffer that is large enough to hold the
1168 /// spelling of the filename, but is also expected to handle the case when
1169 /// this method decides to use a different buffer.
1170 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1171                                               StringRef &Buffer) {
1172   // Get the text form of the filename.
1173   assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1174
1175   // Make sure the filename is <x> or "x".
1176   bool isAngled;
1177   if (Buffer[0] == '<') {
1178     if (Buffer.back() != '>') {
1179       Diag(Loc, diag::err_pp_expects_filename);
1180       Buffer = StringRef();
1181       return true;
1182     }
1183     isAngled = true;
1184   } else if (Buffer[0] == '"') {
1185     if (Buffer.back() != '"') {
1186       Diag(Loc, diag::err_pp_expects_filename);
1187       Buffer = StringRef();
1188       return true;
1189     }
1190     isAngled = false;
1191   } else {
1192     Diag(Loc, diag::err_pp_expects_filename);
1193     Buffer = StringRef();
1194     return true;
1195   }
1196
1197   // Diagnose #include "" as invalid.
1198   if (Buffer.size() <= 2) {
1199     Diag(Loc, diag::err_pp_empty_filename);
1200     Buffer = StringRef();
1201     return true;
1202   }
1203
1204   // Skip the brackets.
1205   Buffer = Buffer.substr(1, Buffer.size()-2);
1206   return isAngled;
1207 }
1208
1209 /// \brief Handle cases where the \#include name is expanded from a macro
1210 /// as multiple tokens, which need to be glued together.
1211 ///
1212 /// This occurs for code like:
1213 /// \code
1214 ///    \#define FOO <a/b.h>
1215 ///    \#include FOO
1216 /// \endcode
1217 /// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1218 ///
1219 /// This code concatenates and consumes tokens up to the '>' token.  It returns
1220 /// false if the > was found, otherwise it returns true if it finds and consumes
1221 /// the EOD marker.
1222 bool Preprocessor::ConcatenateIncludeName(
1223                                         SmallString<128> &FilenameBuffer,
1224                                           SourceLocation &End) {
1225   Token CurTok;
1226
1227   Lex(CurTok);
1228   while (CurTok.isNot(tok::eod)) {
1229     End = CurTok.getLocation();
1230     
1231     // FIXME: Provide code completion for #includes.
1232     if (CurTok.is(tok::code_completion)) {
1233       setCodeCompletionReached();
1234       Lex(CurTok);
1235       continue;
1236     }
1237
1238     // Append the spelling of this token to the buffer. If there was a space
1239     // before it, add it now.
1240     if (CurTok.hasLeadingSpace())
1241       FilenameBuffer.push_back(' ');
1242
1243     // Get the spelling of the token, directly into FilenameBuffer if possible.
1244     unsigned PreAppendSize = FilenameBuffer.size();
1245     FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1246
1247     const char *BufPtr = &FilenameBuffer[PreAppendSize];
1248     unsigned ActualLen = getSpelling(CurTok, BufPtr);
1249
1250     // If the token was spelled somewhere else, copy it into FilenameBuffer.
1251     if (BufPtr != &FilenameBuffer[PreAppendSize])
1252       memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1253
1254     // Resize FilenameBuffer to the correct size.
1255     if (CurTok.getLength() != ActualLen)
1256       FilenameBuffer.resize(PreAppendSize+ActualLen);
1257
1258     // If we found the '>' marker, return success.
1259     if (CurTok.is(tok::greater))
1260       return false;
1261
1262     Lex(CurTok);
1263   }
1264
1265   // If we hit the eod marker, emit an error and return true so that the caller
1266   // knows the EOD has been read.
1267   Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1268   return true;
1269 }
1270
1271 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1272 /// the file to be included from the lexer, then include it!  This is a common
1273 /// routine with functionality shared between \#include, \#include_next and
1274 /// \#import.  LookupFrom is set when this is a \#include_next directive, it
1275 /// specifies the file to start searching from.
1276 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, 
1277                                           Token &IncludeTok,
1278                                           const DirectoryLookup *LookupFrom,
1279                                           bool isImport) {
1280
1281   Token FilenameTok;
1282   CurPPLexer->LexIncludeFilename(FilenameTok);
1283
1284   // Reserve a buffer to get the spelling.
1285   SmallString<128> FilenameBuffer;
1286   StringRef Filename;
1287   SourceLocation End;
1288   SourceLocation CharEnd; // the end of this directive, in characters
1289   
1290   switch (FilenameTok.getKind()) {
1291   case tok::eod:
1292     // If the token kind is EOD, the error has already been diagnosed.
1293     return;
1294
1295   case tok::angle_string_literal:
1296   case tok::string_literal:
1297     Filename = getSpelling(FilenameTok, FilenameBuffer);
1298     End = FilenameTok.getLocation();
1299     CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1300     break;
1301
1302   case tok::less:
1303     // This could be a <foo/bar.h> file coming from a macro expansion.  In this
1304     // case, glue the tokens together into FilenameBuffer and interpret those.
1305     FilenameBuffer.push_back('<');
1306     if (ConcatenateIncludeName(FilenameBuffer, End))
1307       return;   // Found <eod> but no ">"?  Diagnostic already emitted.
1308     Filename = FilenameBuffer.str();
1309     CharEnd = End.getLocWithOffset(1);
1310     break;
1311   default:
1312     Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1313     DiscardUntilEndOfDirective();
1314     return;
1315   }
1316
1317   CharSourceRange FilenameRange
1318     = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1319   StringRef OriginalFilename = Filename;
1320   bool isAngled =
1321     GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1322   // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1323   // error.
1324   if (Filename.empty()) {
1325     DiscardUntilEndOfDirective();
1326     return;
1327   }
1328
1329   // Verify that there is nothing after the filename, other than EOD.  Note that
1330   // we allow macros that expand to nothing after the filename, because this
1331   // falls into the category of "#include pp-tokens new-line" specified in
1332   // C99 6.10.2p4.
1333   CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1334
1335   // Check that we don't have infinite #include recursion.
1336   if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1337     Diag(FilenameTok, diag::err_pp_include_too_deep);
1338     return;
1339   }
1340
1341   // Complain about attempts to #include files in an audit pragma.
1342   if (PragmaARCCFCodeAuditedLoc.isValid()) {
1343     Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1344     Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1345
1346     // Immediately leave the pragma.
1347     PragmaARCCFCodeAuditedLoc = SourceLocation();
1348   }
1349
1350   if (HeaderInfo.HasIncludeAliasMap()) {
1351     // Map the filename with the brackets still attached.  If the name doesn't 
1352     // map to anything, fall back on the filename we've already gotten the 
1353     // spelling for.
1354     StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1355     if (!NewName.empty())
1356       Filename = NewName;
1357   }
1358
1359   // Search include directories.
1360   const DirectoryLookup *CurDir;
1361   SmallString<1024> SearchPath;
1362   SmallString<1024> RelativePath;
1363   // We get the raw path only if we have 'Callbacks' to which we later pass
1364   // the path.
1365   Module *SuggestedModule = 0;
1366   const FileEntry *File = LookupFile(
1367       Filename, isAngled, LookupFrom, CurDir,
1368       Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL,
1369       getLangOpts().Modules? &SuggestedModule : 0);
1370
1371   if (Callbacks) {
1372     if (!File) {
1373       // Give the clients a chance to recover.
1374       SmallString<128> RecoveryPath;
1375       if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1376         if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1377           // Add the recovery path to the list of search paths.
1378           DirectoryLookup DL(DE, SrcMgr::C_User, true, false);
1379           HeaderInfo.AddSearchPath(DL, isAngled);
1380           
1381           // Try the lookup again, skipping the cache.
1382           File = LookupFile(Filename, isAngled, LookupFrom, CurDir, 0, 0,
1383                             getLangOpts().Modules? &SuggestedModule : 0,
1384                             /*SkipCache*/true);
1385         }
1386       }
1387     }
1388     
1389     if (!SuggestedModule) {
1390       // Notify the callback object that we've seen an inclusion directive.
1391       Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1392                                     FilenameRange, File,
1393                                     SearchPath, RelativePath,
1394                                     /*ImportedModule=*/0);
1395     }
1396   }
1397   
1398   if (File == 0) {
1399     if (!SuppressIncludeNotFoundError) {
1400       // If the file could not be located and it was included via angle 
1401       // brackets, we can attempt a lookup as though it were a quoted path to
1402       // provide the user with a possible fixit.
1403       if (isAngled) {
1404         File = LookupFile(Filename, false, LookupFrom, CurDir, 
1405                           Callbacks ? &SearchPath : 0, 
1406                           Callbacks ? &RelativePath : 0, 
1407                           getLangOpts().Modules ? &SuggestedModule : 0);
1408         if (File) {
1409           SourceRange Range(FilenameTok.getLocation(), CharEnd);
1410           Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) << 
1411             Filename << 
1412             FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1413         }
1414       }
1415       // If the file is still not found, just go with the vanilla diagnostic
1416       if (!File)
1417         Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1418     }
1419     if (!File)
1420       return;
1421   }
1422
1423   // If we are supposed to import a module rather than including the header,
1424   // do so now.
1425   if (SuggestedModule) {
1426     // Compute the module access path corresponding to this module.
1427     // FIXME: Should we have a second loadModule() overload to avoid this
1428     // extra lookup step?
1429     llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1430     for (Module *Mod = SuggestedModule; Mod; Mod = Mod->Parent)
1431       Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1432                                     FilenameTok.getLocation()));
1433     std::reverse(Path.begin(), Path.end());
1434
1435     // Warn that we're replacing the include/import with a module import.
1436     SmallString<128> PathString;
1437     for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1438       if (I)
1439         PathString += '.';
1440       PathString += Path[I].first->getName();
1441     }
1442     int IncludeKind = 0;
1443     
1444     switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1445     case tok::pp_include:
1446       IncludeKind = 0;
1447       break;
1448       
1449     case tok::pp_import:
1450       IncludeKind = 1;
1451       break;        
1452         
1453     case tok::pp_include_next:
1454       IncludeKind = 2;
1455       break;
1456         
1457     case tok::pp___include_macros:
1458       IncludeKind = 3;
1459       break;
1460         
1461     default:
1462       llvm_unreachable("unknown include directive kind");
1463     }
1464
1465     // Determine whether we are actually building the module that this
1466     // include directive maps to.
1467     bool BuildingImportedModule
1468       = Path[0].first->getName() == getLangOpts().CurrentModule;
1469     
1470     if (!BuildingImportedModule && getLangOpts().ObjC2) {
1471       // If we're not building the imported module, warn that we're going
1472       // to automatically turn this inclusion directive into a module import.
1473       // We only do this in Objective-C, where we have a module-import syntax.
1474       CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd), 
1475                                    /*IsTokenRange=*/false);
1476       Diag(HashLoc, diag::warn_auto_module_import)
1477         << IncludeKind << PathString 
1478         << FixItHint::CreateReplacement(ReplaceRange,
1479              "@__experimental_modules_import " + PathString.str().str() + ";");
1480     }
1481     
1482     // Load the module.
1483     // If this was an #__include_macros directive, only make macros visible.
1484     Module::NameVisibilityKind Visibility 
1485       = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible;
1486     Module *Imported
1487       = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1488                                    /*IsIncludeDirective=*/true);
1489     assert((Imported == 0 || Imported == SuggestedModule) &&
1490            "the imported module is different than the suggested one");
1491     
1492     // If this header isn't part of the module we're building, we're done.
1493     if (!BuildingImportedModule && Imported) {
1494       if (Callbacks) {
1495         Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1496                                       FilenameRange, File,
1497                                       SearchPath, RelativePath, Imported);
1498       }
1499       return;
1500     }
1501   }
1502
1503   if (Callbacks && SuggestedModule) {
1504     // We didn't notify the callback object that we've seen an inclusion
1505     // directive before. Now that we are parsing the include normally and not
1506     // turning it to a module import, notify the callback object.
1507     Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1508                                   FilenameRange, File,
1509                                   SearchPath, RelativePath,
1510                                   /*ImportedModule=*/0);
1511   }
1512   
1513   // The #included file will be considered to be a system header if either it is
1514   // in a system include directory, or if the #includer is a system include
1515   // header.
1516   SrcMgr::CharacteristicKind FileCharacter =
1517     std::max(HeaderInfo.getFileDirFlavor(File),
1518              SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1519
1520   // Ask HeaderInfo if we should enter this #include file.  If not, #including
1521   // this file will have no effect.
1522   if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1523     if (Callbacks)
1524       Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1525     return;
1526   }
1527
1528   // Look up the file, create a File ID for it.
1529   SourceLocation IncludePos = End;
1530   // If the filename string was the result of macro expansions, set the include
1531   // position on the file where it will be included and after the expansions.
1532   if (IncludePos.isMacroID())
1533     IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1534   FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
1535   assert(!FID.isInvalid() && "Expected valid file ID");
1536
1537   // Finally, if all is good, enter the new file!
1538   EnterSourceFile(FID, CurDir, FilenameTok.getLocation());
1539 }
1540
1541 /// HandleIncludeNextDirective - Implements \#include_next.
1542 ///
1543 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1544                                               Token &IncludeNextTok) {
1545   Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1546
1547   // #include_next is like #include, except that we start searching after
1548   // the current found directory.  If we can't do this, issue a
1549   // diagnostic.
1550   const DirectoryLookup *Lookup = CurDirLookup;
1551   if (isInPrimaryFile()) {
1552     Lookup = 0;
1553     Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1554   } else if (Lookup == 0) {
1555     Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1556   } else {
1557     // Start looking up in the next directory.
1558     ++Lookup;
1559   }
1560
1561   return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
1562 }
1563
1564 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
1565 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1566   // The Microsoft #import directive takes a type library and generates header
1567   // files from it, and includes those.  This is beyond the scope of what clang
1568   // does, so we ignore it and error out.  However, #import can optionally have
1569   // trailing attributes that span multiple lines.  We're going to eat those
1570   // so we can continue processing from there.
1571   Diag(Tok, diag::err_pp_import_directive_ms );
1572
1573   // Read tokens until we get to the end of the directive.  Note that the 
1574   // directive can be split over multiple lines using the backslash character.
1575   DiscardUntilEndOfDirective();
1576 }
1577
1578 /// HandleImportDirective - Implements \#import.
1579 ///
1580 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1581                                          Token &ImportTok) {
1582   if (!LangOpts.ObjC1) {  // #import is standard for ObjC.
1583     if (LangOpts.MicrosoftMode)
1584       return HandleMicrosoftImportDirective(ImportTok);
1585     Diag(ImportTok, diag::ext_pp_import_directive);
1586   }
1587   return HandleIncludeDirective(HashLoc, ImportTok, 0, true);
1588 }
1589
1590 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1591 /// pseudo directive in the predefines buffer.  This handles it by sucking all
1592 /// tokens through the preprocessor and discarding them (only keeping the side
1593 /// effects on the preprocessor).
1594 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1595                                                 Token &IncludeMacrosTok) {
1596   // This directive should only occur in the predefines buffer.  If not, emit an
1597   // error and reject it.
1598   SourceLocation Loc = IncludeMacrosTok.getLocation();
1599   if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1600     Diag(IncludeMacrosTok.getLocation(),
1601          diag::pp_include_macros_out_of_predefines);
1602     DiscardUntilEndOfDirective();
1603     return;
1604   }
1605
1606   // Treat this as a normal #include for checking purposes.  If this is
1607   // successful, it will push a new lexer onto the include stack.
1608   HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false);
1609
1610   Token TmpTok;
1611   do {
1612     Lex(TmpTok);
1613     assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1614   } while (TmpTok.isNot(tok::hashhash));
1615 }
1616
1617 //===----------------------------------------------------------------------===//
1618 // Preprocessor Macro Directive Handling.
1619 //===----------------------------------------------------------------------===//
1620
1621 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1622 /// definition has just been read.  Lex the rest of the arguments and the
1623 /// closing ), updating MI with what we learn.  Return true if an error occurs
1624 /// parsing the arg list.
1625 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
1626   SmallVector<IdentifierInfo*, 32> Arguments;
1627
1628   while (1) {
1629     LexUnexpandedToken(Tok);
1630     switch (Tok.getKind()) {
1631     case tok::r_paren:
1632       // Found the end of the argument list.
1633       if (Arguments.empty())  // #define FOO()
1634         return false;
1635       // Otherwise we have #define FOO(A,)
1636       Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1637       return true;
1638     case tok::ellipsis:  // #define X(... -> C99 varargs
1639       if (!LangOpts.C99)
1640         Diag(Tok, LangOpts.CPlusPlus0x ? 
1641              diag::warn_cxx98_compat_variadic_macro :
1642              diag::ext_variadic_macro);
1643
1644       // Lex the token after the identifier.
1645       LexUnexpandedToken(Tok);
1646       if (Tok.isNot(tok::r_paren)) {
1647         Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1648         return true;
1649       }
1650       // Add the __VA_ARGS__ identifier as an argument.
1651       Arguments.push_back(Ident__VA_ARGS__);
1652       MI->setIsC99Varargs();
1653       MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1654       return false;
1655     case tok::eod:  // #define X(
1656       Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1657       return true;
1658     default:
1659       // Handle keywords and identifiers here to accept things like
1660       // #define Foo(for) for.
1661       IdentifierInfo *II = Tok.getIdentifierInfo();
1662       if (II == 0) {
1663         // #define X(1
1664         Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1665         return true;
1666       }
1667
1668       // If this is already used as an argument, it is used multiple times (e.g.
1669       // #define X(A,A.
1670       if (std::find(Arguments.begin(), Arguments.end(), II) !=
1671           Arguments.end()) {  // C99 6.10.3p6
1672         Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1673         return true;
1674       }
1675
1676       // Add the argument to the macro info.
1677       Arguments.push_back(II);
1678
1679       // Lex the token after the identifier.
1680       LexUnexpandedToken(Tok);
1681
1682       switch (Tok.getKind()) {
1683       default:          // #define X(A B
1684         Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1685         return true;
1686       case tok::r_paren: // #define X(A)
1687         MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1688         return false;
1689       case tok::comma:  // #define X(A,
1690         break;
1691       case tok::ellipsis:  // #define X(A... -> GCC extension
1692         // Diagnose extension.
1693         Diag(Tok, diag::ext_named_variadic_macro);
1694
1695         // Lex the token after the identifier.
1696         LexUnexpandedToken(Tok);
1697         if (Tok.isNot(tok::r_paren)) {
1698           Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1699           return true;
1700         }
1701
1702         MI->setIsGNUVarargs();
1703         MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1704         return false;
1705       }
1706     }
1707   }
1708 }
1709
1710 /// HandleDefineDirective - Implements \#define.  This consumes the entire macro
1711 /// line then lets the caller lex the next real token.
1712 void Preprocessor::HandleDefineDirective(Token &DefineTok) {
1713   ++NumDefined;
1714
1715   Token MacroNameTok;
1716   ReadMacroName(MacroNameTok, 1);
1717
1718   // Error reading macro name?  If so, diagnostic already issued.
1719   if (MacroNameTok.is(tok::eod))
1720     return;
1721
1722   Token LastTok = MacroNameTok;
1723
1724   // If we are supposed to keep comments in #defines, reenable comment saving
1725   // mode.
1726   if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
1727
1728   // Create the new macro.
1729   MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
1730
1731   Token Tok;
1732   LexUnexpandedToken(Tok);
1733
1734   // If this is a function-like macro definition, parse the argument list,
1735   // marking each of the identifiers as being used as macro arguments.  Also,
1736   // check other constraints on the first token of the macro body.
1737   if (Tok.is(tok::eod)) {
1738     // If there is no body to this macro, we have no special handling here.
1739   } else if (Tok.hasLeadingSpace()) {
1740     // This is a normal token with leading space.  Clear the leading space
1741     // marker on the first token to get proper expansion.
1742     Tok.clearFlag(Token::LeadingSpace);
1743   } else if (Tok.is(tok::l_paren)) {
1744     // This is a function-like macro definition.  Read the argument list.
1745     MI->setIsFunctionLike();
1746     if (ReadMacroDefinitionArgList(MI, LastTok)) {
1747       // Forget about MI.
1748       ReleaseMacroInfo(MI);
1749       // Throw away the rest of the line.
1750       if (CurPPLexer->ParsingPreprocessorDirective)
1751         DiscardUntilEndOfDirective();
1752       return;
1753     }
1754
1755     // If this is a definition of a variadic C99 function-like macro, not using
1756     // the GNU named varargs extension, enabled __VA_ARGS__.
1757
1758     // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1759     // This gets unpoisoned where it is allowed.
1760     assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1761     if (MI->isC99Varargs())
1762       Ident__VA_ARGS__->setIsPoisoned(false);
1763
1764     // Read the first token after the arg list for down below.
1765     LexUnexpandedToken(Tok);
1766   } else if (LangOpts.C99 || LangOpts.CPlusPlus0x) {
1767     // C99 requires whitespace between the macro definition and the body.  Emit
1768     // a diagnostic for something like "#define X+".
1769     Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
1770   } else {
1771     // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1772     // first character of a replacement list is not a character required by
1773     // subclause 5.2.1, then there shall be white-space separation between the
1774     // identifier and the replacement list.".  5.2.1 lists this set:
1775     //   "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1776     // is irrelevant here.
1777     bool isInvalid = false;
1778     if (Tok.is(tok::at)) // @ is not in the list above.
1779       isInvalid = true;
1780     else if (Tok.is(tok::unknown)) {
1781       // If we have an unknown token, it is something strange like "`".  Since
1782       // all of valid characters would have lexed into a single character
1783       // token of some sort, we know this is not a valid case.
1784       isInvalid = true;
1785     }
1786     if (isInvalid)
1787       Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1788     else
1789       Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
1790   }
1791
1792   if (!Tok.is(tok::eod))
1793     LastTok = Tok;
1794
1795   // Read the rest of the macro body.
1796   if (MI->isObjectLike()) {
1797     // Object-like macros are very simple, just read their body.
1798     while (Tok.isNot(tok::eod)) {
1799       LastTok = Tok;
1800       MI->AddTokenToBody(Tok);
1801       // Get the next token of the macro.
1802       LexUnexpandedToken(Tok);
1803     }
1804
1805   } else {
1806     // Otherwise, read the body of a function-like macro.  While we are at it,
1807     // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1808     // parameters in function-like macro expansions.
1809     while (Tok.isNot(tok::eod)) {
1810       LastTok = Tok;
1811
1812       if (Tok.isNot(tok::hash)) {
1813         MI->AddTokenToBody(Tok);
1814
1815         // Get the next token of the macro.
1816         LexUnexpandedToken(Tok);
1817         continue;
1818       }
1819
1820       // Get the next token of the macro.
1821       LexUnexpandedToken(Tok);
1822
1823       // Check for a valid macro arg identifier.
1824       if (Tok.getIdentifierInfo() == 0 ||
1825           MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1826
1827         // If this is assembler-with-cpp mode, we accept random gibberish after
1828         // the '#' because '#' is often a comment character.  However, change
1829         // the kind of the token to tok::unknown so that the preprocessor isn't
1830         // confused.
1831         if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
1832           LastTok.setKind(tok::unknown);
1833         } else {
1834           Diag(Tok, diag::err_pp_stringize_not_parameter);
1835           ReleaseMacroInfo(MI);
1836
1837           // Disable __VA_ARGS__ again.
1838           Ident__VA_ARGS__->setIsPoisoned(true);
1839           return;
1840         }
1841       }
1842
1843       // Things look ok, add the '#' and param name tokens to the macro.
1844       MI->AddTokenToBody(LastTok);
1845       MI->AddTokenToBody(Tok);
1846       LastTok = Tok;
1847
1848       // Get the next token of the macro.
1849       LexUnexpandedToken(Tok);
1850     }
1851   }
1852
1853
1854   // Disable __VA_ARGS__ again.
1855   Ident__VA_ARGS__->setIsPoisoned(true);
1856
1857   // Check that there is no paste (##) operator at the beginning or end of the
1858   // replacement list.
1859   unsigned NumTokens = MI->getNumTokens();
1860   if (NumTokens != 0) {
1861     if (MI->getReplacementToken(0).is(tok::hashhash)) {
1862       Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
1863       ReleaseMacroInfo(MI);
1864       return;
1865     }
1866     if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
1867       Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
1868       ReleaseMacroInfo(MI);
1869       return;
1870     }
1871   }
1872
1873   MI->setDefinitionEndLoc(LastTok.getLocation());
1874
1875   // Finally, if this identifier already had a macro defined for it, verify that
1876   // the macro bodies are identical, and issue diagnostics if they are not.
1877   if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
1878     // It is very common for system headers to have tons of macro redefinitions
1879     // and for warnings to be disabled in system headers.  If this is the case,
1880     // then don't bother calling MacroInfo::isIdenticalTo.
1881     if (!getDiagnostics().getSuppressSystemWarnings() ||
1882         !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
1883       if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
1884         Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1885
1886       // Macros must be identical.  This means all tokens and whitespace
1887       // separation must be the same.  C99 6.10.3.2.
1888       if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
1889           !MI->isIdenticalTo(*OtherMI, *this)) {
1890         Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
1891           << MacroNameTok.getIdentifierInfo();
1892         Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
1893       }
1894     }
1895     if (OtherMI->isWarnIfUnused())
1896       WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
1897   }
1898
1899   setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
1900
1901   assert(!MI->isUsed());
1902   // If we need warning for not using the macro, add its location in the
1903   // warn-because-unused-macro set. If it gets used it will be removed from set.
1904   if (isInPrimaryFile() && // don't warn for include'd macros.
1905       Diags->getDiagnosticLevel(diag::pp_macro_not_used,
1906           MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) {
1907     MI->setIsWarnIfUnused(true);
1908     WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
1909   }
1910
1911   // If the callbacks want to know, tell them about the macro definition.
1912   if (Callbacks)
1913     Callbacks->MacroDefined(MacroNameTok, MI);
1914 }
1915
1916 /// HandleUndefDirective - Implements \#undef.
1917 ///
1918 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
1919   ++NumUndefined;
1920
1921   Token MacroNameTok;
1922   ReadMacroName(MacroNameTok, 2);
1923
1924   // Error reading macro name?  If so, diagnostic already issued.
1925   if (MacroNameTok.is(tok::eod))
1926     return;
1927
1928   // Check to see if this is the last token on the #undef line.
1929   CheckEndOfDirective("undef");
1930
1931   // Okay, we finally have a valid identifier to undef.
1932   MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
1933
1934   // If the macro is not defined, this is a noop undef, just return.
1935   if (MI == 0) return;
1936
1937   if (!MI->isUsed() && MI->isWarnIfUnused())
1938     Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
1939
1940   // If the callbacks want to know, tell them about the macro #undef.
1941   if (Callbacks)
1942     Callbacks->MacroUndefined(MacroNameTok, MI);
1943
1944   if (MI->isWarnIfUnused())
1945     WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1946
1947   UndefineMacro(MacroNameTok.getIdentifierInfo(), MI,
1948                 MacroNameTok.getLocation());
1949 }
1950
1951 void Preprocessor::UndefineMacro(IdentifierInfo *II, MacroInfo *MI,
1952                                  SourceLocation UndefLoc) {
1953   MI->setUndefLoc(UndefLoc);
1954   if (MI->isFromAST()) {
1955     MI->setChangedAfterLoad();
1956     if (Listener)
1957       Listener->UndefinedMacro(MI);
1958   }
1959
1960   clearMacroInfo(II);
1961 }
1962
1963
1964 //===----------------------------------------------------------------------===//
1965 // Preprocessor Conditional Directive Handling.
1966 //===----------------------------------------------------------------------===//
1967
1968 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive.  isIfndef
1969 /// is true when this is a \#ifndef directive.  ReadAnyTokensBeforeDirective is
1970 /// true if any tokens have been returned or pp-directives activated before this
1971 /// \#ifndef has been lexed.
1972 ///
1973 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
1974                                         bool ReadAnyTokensBeforeDirective) {
1975   ++NumIf;
1976   Token DirectiveTok = Result;
1977
1978   Token MacroNameTok;
1979   ReadMacroName(MacroNameTok);
1980
1981   // Error reading macro name?  If so, diagnostic already issued.
1982   if (MacroNameTok.is(tok::eod)) {
1983     // Skip code until we get to #endif.  This helps with recovery by not
1984     // emitting an error when the #endif is reached.
1985     SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1986                                  /*Foundnonskip*/false, /*FoundElse*/false);
1987     return;
1988   }
1989
1990   // Check to see if this is the last token on the #if[n]def line.
1991   CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
1992
1993   IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1994   MacroInfo *MI = getMacroInfo(MII);
1995
1996   if (CurPPLexer->getConditionalStackDepth() == 0) {
1997     // If the start of a top-level #ifdef and if the macro is not defined,
1998     // inform MIOpt that this might be the start of a proper include guard.
1999     // Otherwise it is some other form of unknown conditional which we can't
2000     // handle.
2001     if (!ReadAnyTokensBeforeDirective && MI == 0) {
2002       assert(isIfndef && "#ifdef shouldn't reach here");
2003       CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII);
2004     } else
2005       CurPPLexer->MIOpt.EnterTopLevelConditional();
2006   }
2007
2008   // If there is a macro, process it.
2009   if (MI)  // Mark it used.
2010     markMacroAsUsed(MI);
2011
2012   if (Callbacks) {
2013     if (isIfndef)
2014       Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok);
2015     else
2016       Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok);
2017   }
2018
2019   // Should we include the stuff contained by this directive?
2020   if (!MI == isIfndef) {
2021     // Yes, remember that we are inside a conditional, then lex the next token.
2022     CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2023                                      /*wasskip*/false, /*foundnonskip*/true,
2024                                      /*foundelse*/false);
2025   } else {
2026     // No, skip the contents of this block.
2027     SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2028                                  /*Foundnonskip*/false,
2029                                  /*FoundElse*/false);
2030   }
2031 }
2032
2033 /// HandleIfDirective - Implements the \#if directive.
2034 ///
2035 void Preprocessor::HandleIfDirective(Token &IfToken,
2036                                      bool ReadAnyTokensBeforeDirective) {
2037   ++NumIf;
2038
2039   // Parse and evaluate the conditional expression.
2040   IdentifierInfo *IfNDefMacro = 0;
2041   const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2042   const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2043   const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2044
2045   // If this condition is equivalent to #ifndef X, and if this is the first
2046   // directive seen, handle it for the multiple-include optimization.
2047   if (CurPPLexer->getConditionalStackDepth() == 0) {
2048     if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2049       CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2050     else
2051       CurPPLexer->MIOpt.EnterTopLevelConditional();
2052   }
2053
2054   if (Callbacks)
2055     Callbacks->If(IfToken.getLocation(),
2056                   SourceRange(ConditionalBegin, ConditionalEnd));
2057
2058   // Should we include the stuff contained by this directive?
2059   if (ConditionalTrue) {
2060     // Yes, remember that we are inside a conditional, then lex the next token.
2061     CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2062                                    /*foundnonskip*/true, /*foundelse*/false);
2063   } else {
2064     // No, skip the contents of this block.
2065     SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2066                                  /*FoundElse*/false);
2067   }
2068 }
2069
2070 /// HandleEndifDirective - Implements the \#endif directive.
2071 ///
2072 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2073   ++NumEndif;
2074
2075   // Check that this is the whole directive.
2076   CheckEndOfDirective("endif");
2077
2078   PPConditionalInfo CondInfo;
2079   if (CurPPLexer->popConditionalLevel(CondInfo)) {
2080     // No conditionals on the stack: this is an #endif without an #if.
2081     Diag(EndifToken, diag::err_pp_endif_without_if);
2082     return;
2083   }
2084
2085   // If this the end of a top-level #endif, inform MIOpt.
2086   if (CurPPLexer->getConditionalStackDepth() == 0)
2087     CurPPLexer->MIOpt.ExitTopLevelConditional();
2088
2089   assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2090          "This code should only be reachable in the non-skipping case!");
2091
2092   if (Callbacks)
2093     Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2094 }
2095
2096 /// HandleElseDirective - Implements the \#else directive.
2097 ///
2098 void Preprocessor::HandleElseDirective(Token &Result) {
2099   ++NumElse;
2100
2101   // #else directive in a non-skipping conditional... start skipping.
2102   CheckEndOfDirective("else");
2103
2104   PPConditionalInfo CI;
2105   if (CurPPLexer->popConditionalLevel(CI)) {
2106     Diag(Result, diag::pp_err_else_without_if);
2107     return;
2108   }
2109
2110   // If this is a top-level #else, inform the MIOpt.
2111   if (CurPPLexer->getConditionalStackDepth() == 0)
2112     CurPPLexer->MIOpt.EnterTopLevelConditional();
2113
2114   // If this is a #else with a #else before it, report the error.
2115   if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2116
2117   if (Callbacks)
2118     Callbacks->Else(Result.getLocation(), CI.IfLoc);
2119
2120   // Finally, skip the rest of the contents of this block.
2121   SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2122                                /*FoundElse*/true, Result.getLocation());
2123 }
2124
2125 /// HandleElifDirective - Implements the \#elif directive.
2126 ///
2127 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2128   ++NumElse;
2129
2130   // #elif directive in a non-skipping conditional... start skipping.
2131   // We don't care what the condition is, because we will always skip it (since
2132   // the block immediately before it was included).
2133   const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2134   DiscardUntilEndOfDirective();
2135   const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2136
2137   PPConditionalInfo CI;
2138   if (CurPPLexer->popConditionalLevel(CI)) {
2139     Diag(ElifToken, diag::pp_err_elif_without_if);
2140     return;
2141   }
2142
2143   // If this is a top-level #elif, inform the MIOpt.
2144   if (CurPPLexer->getConditionalStackDepth() == 0)
2145     CurPPLexer->MIOpt.EnterTopLevelConditional();
2146
2147   // If this is a #elif with a #else before it, report the error.
2148   if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2149   
2150   if (Callbacks)
2151     Callbacks->Elif(ElifToken.getLocation(),
2152                     SourceRange(ConditionalBegin, ConditionalEnd), CI.IfLoc);
2153
2154   // Finally, skip the rest of the contents of this block.
2155   SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2156                                /*FoundElse*/CI.FoundElse,
2157                                ElifToken.getLocation());
2158 }