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