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