clang API Documentation

PPDirectives.cpp
Go to the documentation of this file.
00001 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements # directive processing for the Preprocessor.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Lex/Preprocessor.h"
00015 #include "clang/Lex/LiteralSupport.h"
00016 #include "clang/Lex/HeaderSearch.h"
00017 #include "clang/Lex/MacroInfo.h"
00018 #include "clang/Lex/LexDiagnostic.h"
00019 #include "clang/Lex/CodeCompletionHandler.h"
00020 #include "clang/Lex/ModuleLoader.h"
00021 #include "clang/Lex/Pragma.h"
00022 #include "clang/Basic/FileManager.h"
00023 #include "clang/Basic/SourceManager.h"
00024 #include "llvm/ADT/APInt.h"
00025 #include "llvm/Support/ErrorHandling.h"
00026 using namespace clang;
00027 
00028 //===----------------------------------------------------------------------===//
00029 // Utility Methods for Preprocessor Directive Handling.
00030 //===----------------------------------------------------------------------===//
00031 
00032 MacroInfo *Preprocessor::AllocateMacroInfo() {
00033   MacroInfoChain *MIChain;
00034 
00035   if (MICache) {
00036     MIChain = MICache;
00037     MICache = MICache->Next;
00038   }
00039   else {
00040     MIChain = BP.Allocate<MacroInfoChain>();
00041   }
00042 
00043   MIChain->Next = MIChainHead;
00044   MIChain->Prev = 0;
00045   if (MIChainHead)
00046     MIChainHead->Prev = MIChain;
00047   MIChainHead = MIChain;
00048 
00049   return &(MIChain->MI);
00050 }
00051 
00052 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
00053   MacroInfo *MI = AllocateMacroInfo();
00054   new (MI) MacroInfo(L);
00055   return MI;
00056 }
00057 
00058 MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) {
00059   MacroInfo *MI = AllocateMacroInfo();
00060   new (MI) MacroInfo(MacroToClone, BP);
00061   return MI;
00062 }
00063 
00064 /// ReleaseMacroInfo - Release the specified MacroInfo.  This memory will
00065 ///  be reused for allocating new MacroInfo objects.
00066 void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) {
00067   MacroInfoChain *MIChain = (MacroInfoChain*) MI;
00068   if (MacroInfoChain *Prev = MIChain->Prev) {
00069     MacroInfoChain *Next = MIChain->Next;
00070     Prev->Next = Next;
00071     if (Next)
00072       Next->Prev = Prev;
00073   }
00074   else {
00075     assert(MIChainHead == MIChain);
00076     MIChainHead = MIChain->Next;
00077     MIChainHead->Prev = 0;
00078   }
00079   MIChain->Next = MICache;
00080   MICache = MIChain;
00081 
00082   MI->Destroy();
00083 }
00084 
00085 /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
00086 /// current line until the tok::eod token is found.
00087 void Preprocessor::DiscardUntilEndOfDirective() {
00088   Token Tmp;
00089   do {
00090     LexUnexpandedToken(Tmp);
00091     assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
00092   } while (Tmp.isNot(tok::eod));
00093 }
00094 
00095 /// ReadMacroName - Lex and validate a macro name, which occurs after a
00096 /// #define or #undef.  This sets the token kind to eod and discards the rest
00097 /// of the macro line if the macro name is invalid.  isDefineUndef is 1 if
00098 /// this is due to a a #define, 2 if #undef directive, 0 if it is something
00099 /// else (e.g. #ifdef).
00100 void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
00101   // Read the token, don't allow macro expansion on it.
00102   LexUnexpandedToken(MacroNameTok);
00103 
00104   if (MacroNameTok.is(tok::code_completion)) {
00105     if (CodeComplete)
00106       CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
00107     setCodeCompletionReached();
00108     LexUnexpandedToken(MacroNameTok);
00109   }
00110   
00111   // Missing macro name?
00112   if (MacroNameTok.is(tok::eod)) {
00113     Diag(MacroNameTok, diag::err_pp_missing_macro_name);
00114     return;
00115   }
00116 
00117   IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
00118   if (II == 0) {
00119     bool Invalid = false;
00120     std::string Spelling = getSpelling(MacroNameTok, &Invalid);
00121     if (Invalid)
00122       return;
00123 
00124     const IdentifierInfo &Info = Identifiers.get(Spelling);
00125 
00126     // Allow #defining |and| and friends in microsoft mode.
00127     if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MicrosoftMode) {
00128       MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling));
00129       return;
00130     }
00131 
00132     if (Info.isCPlusPlusOperatorKeyword())
00133       // C++ 2.5p2: Alternative tokens behave the same as its primary token
00134       // except for their spellings.
00135       Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling;
00136     else
00137       Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
00138     // Fall through on error.
00139   } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
00140     // Error if defining "defined": C99 6.10.8.4.
00141     Diag(MacroNameTok, diag::err_defined_macro_name);
00142   } else if (isDefineUndef && II->hasMacroDefinition() &&
00143              getMacroInfo(II)->isBuiltinMacro()) {
00144     // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
00145     if (isDefineUndef == 1)
00146       Diag(MacroNameTok, diag::pp_redef_builtin_macro);
00147     else
00148       Diag(MacroNameTok, diag::pp_undef_builtin_macro);
00149   } else {
00150     // Okay, we got a good identifier node.  Return it.
00151     return;
00152   }
00153 
00154   // Invalid macro name, read and discard the rest of the line.  Then set the
00155   // token kind to tok::eod.
00156   MacroNameTok.setKind(tok::eod);
00157   return DiscardUntilEndOfDirective();
00158 }
00159 
00160 /// CheckEndOfDirective - Ensure that the next token is a tok::eod token.  If
00161 /// not, emit a diagnostic and consume up until the eod.  If EnableMacros is
00162 /// true, then we consider macros that expand to zero tokens as being ok.
00163 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
00164   Token Tmp;
00165   // Lex unexpanded tokens for most directives: macros might expand to zero
00166   // tokens, causing us to miss diagnosing invalid lines.  Some directives (like
00167   // #line) allow empty macros.
00168   if (EnableMacros)
00169     Lex(Tmp);
00170   else
00171     LexUnexpandedToken(Tmp);
00172 
00173   // There should be no tokens after the directive, but we allow them as an
00174   // extension.
00175   while (Tmp.is(tok::comment))  // Skip comments in -C mode.
00176     LexUnexpandedToken(Tmp);
00177 
00178   if (Tmp.isNot(tok::eod)) {
00179     // Add a fixit in GNU/C99/C++ mode.  Don't offer a fixit for strict-C89,
00180     // or if this is a macro-style preprocessing directive, because it is more
00181     // trouble than it is worth to insert /**/ and check that there is no /**/
00182     // in the range also.
00183     FixItHint Hint;
00184     if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
00185         !CurTokenLexer)
00186       Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
00187     Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
00188     DiscardUntilEndOfDirective();
00189   }
00190 }
00191 
00192 
00193 
00194 /// SkipExcludedConditionalBlock - We just read a #if or related directive and
00195 /// decided that the subsequent tokens are in the #if'd out portion of the
00196 /// file.  Lex the rest of the file, until we see an #endif.  If
00197 /// FoundNonSkipPortion is true, then we have already emitted code for part of
00198 /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
00199 /// is true, then #else directives are ok, if not, then we have already seen one
00200 /// so a #else directive is a duplicate.  When this returns, the caller can lex
00201 /// the first valid token.
00202 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
00203                                                 bool FoundNonSkipPortion,
00204                                                 bool FoundElse,
00205                                                 SourceLocation ElseLoc) {
00206   ++NumSkipped;
00207   assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?");
00208 
00209   CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
00210                                  FoundNonSkipPortion, FoundElse);
00211 
00212   if (CurPTHLexer) {
00213     PTHSkipExcludedConditionalBlock();
00214     return;
00215   }
00216 
00217   // Enter raw mode to disable identifier lookup (and thus macro expansion),
00218   // disabling warnings, etc.
00219   CurPPLexer->LexingRawMode = true;
00220   Token Tok;
00221   while (1) {
00222     CurLexer->Lex(Tok);
00223 
00224     if (Tok.is(tok::code_completion)) {
00225       if (CodeComplete)
00226         CodeComplete->CodeCompleteInConditionalExclusion();
00227       setCodeCompletionReached();
00228       continue;
00229     }
00230     
00231     // If this is the end of the buffer, we have an error.
00232     if (Tok.is(tok::eof)) {
00233       // Emit errors for each unterminated conditional on the stack, including
00234       // the current one.
00235       while (!CurPPLexer->ConditionalStack.empty()) {
00236         if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
00237           Diag(CurPPLexer->ConditionalStack.back().IfLoc,
00238                diag::err_pp_unterminated_conditional);
00239         CurPPLexer->ConditionalStack.pop_back();
00240       }
00241 
00242       // Just return and let the caller lex after this #include.
00243       break;
00244     }
00245 
00246     // If this token is not a preprocessor directive, just skip it.
00247     if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
00248       continue;
00249 
00250     // We just parsed a # character at the start of a line, so we're in
00251     // directive mode.  Tell the lexer this so any newlines we see will be
00252     // converted into an EOD token (this terminates the macro).
00253     CurPPLexer->ParsingPreprocessorDirective = true;
00254     if (CurLexer) CurLexer->SetCommentRetentionState(false);
00255 
00256 
00257     // Read the next token, the directive flavor.
00258     LexUnexpandedToken(Tok);
00259 
00260     // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
00261     // something bogus), skip it.
00262     if (Tok.isNot(tok::raw_identifier)) {
00263       CurPPLexer->ParsingPreprocessorDirective = false;
00264       // Restore comment saving mode.
00265       if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
00266       continue;
00267     }
00268 
00269     // If the first letter isn't i or e, it isn't intesting to us.  We know that
00270     // this is safe in the face of spelling differences, because there is no way
00271     // to spell an i/e in a strange way that is another letter.  Skipping this
00272     // allows us to avoid looking up the identifier info for #define/#undef and
00273     // other common directives.
00274     const char *RawCharData = Tok.getRawIdentifierData();
00275 
00276     char FirstChar = RawCharData[0];
00277     if (FirstChar >= 'a' && FirstChar <= 'z' &&
00278         FirstChar != 'i' && FirstChar != 'e') {
00279       CurPPLexer->ParsingPreprocessorDirective = false;
00280       // Restore comment saving mode.
00281       if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
00282       continue;
00283     }
00284 
00285     // Get the identifier name without trigraphs or embedded newlines.  Note
00286     // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
00287     // when skipping.
00288     char DirectiveBuf[20];
00289     StringRef Directive;
00290     if (!Tok.needsCleaning() && Tok.getLength() < 20) {
00291       Directive = StringRef(RawCharData, Tok.getLength());
00292     } else {
00293       std::string DirectiveStr = getSpelling(Tok);
00294       unsigned IdLen = DirectiveStr.size();
00295       if (IdLen >= 20) {
00296         CurPPLexer->ParsingPreprocessorDirective = false;
00297         // Restore comment saving mode.
00298         if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
00299         continue;
00300       }
00301       memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
00302       Directive = StringRef(DirectiveBuf, IdLen);
00303     }
00304 
00305     if (Directive.startswith("if")) {
00306       StringRef Sub = Directive.substr(2);
00307       if (Sub.empty() ||   // "if"
00308           Sub == "def" ||   // "ifdef"
00309           Sub == "ndef") {  // "ifndef"
00310         // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
00311         // bother parsing the condition.
00312         DiscardUntilEndOfDirective();
00313         CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
00314                                        /*foundnonskip*/false,
00315                                        /*foundelse*/false);
00316       }
00317     } else if (Directive[0] == 'e') {
00318       StringRef Sub = Directive.substr(1);
00319       if (Sub == "ndif") {  // "endif"
00320         CheckEndOfDirective("endif");
00321         PPConditionalInfo CondInfo;
00322         CondInfo.WasSkipping = true; // Silence bogus warning.
00323         bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
00324         (void)InCond;  // Silence warning in no-asserts mode.
00325         assert(!InCond && "Can't be skipping if not in a conditional!");
00326 
00327         // If we popped the outermost skipping block, we're done skipping!
00328         if (!CondInfo.WasSkipping) {
00329           if (Callbacks)
00330             Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
00331           break;
00332         }
00333       } else if (Sub == "lse") { // "else".
00334         // #else directive in a skipping conditional.  If not in some other
00335         // skipping conditional, and if #else hasn't already been seen, enter it
00336         // as a non-skipping conditional.
00337         PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
00338 
00339         // If this is a #else with a #else before it, report the error.
00340         if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
00341 
00342         // Note that we've seen a #else in this conditional.
00343         CondInfo.FoundElse = true;
00344 
00345         // If the conditional is at the top level, and the #if block wasn't
00346         // entered, enter the #else block now.
00347         if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
00348           CondInfo.FoundNonSkip = true;
00349           CheckEndOfDirective("else");
00350           if (Callbacks)
00351             Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
00352           break;
00353         } else {
00354           DiscardUntilEndOfDirective();  // C99 6.10p4.
00355         }
00356       } else if (Sub == "lif") {  // "elif".
00357         PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
00358 
00359         bool ShouldEnter;
00360         const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
00361         // If this is in a skipping block or if we're already handled this #if
00362         // block, don't bother parsing the condition.
00363         if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
00364           DiscardUntilEndOfDirective();
00365           ShouldEnter = false;
00366         } else {
00367           // Restore the value of LexingRawMode so that identifiers are
00368           // looked up, etc, inside the #elif expression.
00369           assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
00370           CurPPLexer->LexingRawMode = false;
00371           IdentifierInfo *IfNDefMacro = 0;
00372           ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
00373           CurPPLexer->LexingRawMode = true;
00374         }
00375         const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
00376 
00377         // If this is a #elif with a #else before it, report the error.
00378         if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
00379 
00380         // If this condition is true, enter it!
00381         if (ShouldEnter) {
00382           CondInfo.FoundNonSkip = true;
00383           if (Callbacks)
00384             Callbacks->Elif(Tok.getLocation(),
00385                             SourceRange(ConditionalBegin, ConditionalEnd),
00386                             CondInfo.IfLoc);
00387           break;
00388         }
00389       }
00390     }
00391 
00392     CurPPLexer->ParsingPreprocessorDirective = false;
00393     // Restore comment saving mode.
00394     if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
00395   }
00396 
00397   // Finally, if we are out of the conditional (saw an #endif or ran off the end
00398   // of the file, just stop skipping and return to lexing whatever came after
00399   // the #if block.
00400   CurPPLexer->LexingRawMode = false;
00401 
00402   if (Callbacks) {
00403     SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
00404     Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
00405   }
00406 }
00407 
00408 void Preprocessor::PTHSkipExcludedConditionalBlock() {
00409 
00410   while (1) {
00411     assert(CurPTHLexer);
00412     assert(CurPTHLexer->LexingRawMode == false);
00413 
00414     // Skip to the next '#else', '#elif', or #endif.
00415     if (CurPTHLexer->SkipBlock()) {
00416       // We have reached an #endif.  Both the '#' and 'endif' tokens
00417       // have been consumed by the PTHLexer.  Just pop off the condition level.
00418       PPConditionalInfo CondInfo;
00419       bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
00420       (void)InCond;  // Silence warning in no-asserts mode.
00421       assert(!InCond && "Can't be skipping if not in a conditional!");
00422       break;
00423     }
00424 
00425     // We have reached a '#else' or '#elif'.  Lex the next token to get
00426     // the directive flavor.
00427     Token Tok;
00428     LexUnexpandedToken(Tok);
00429 
00430     // We can actually look up the IdentifierInfo here since we aren't in
00431     // raw mode.
00432     tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
00433 
00434     if (K == tok::pp_else) {
00435       // #else: Enter the else condition.  We aren't in a nested condition
00436       //  since we skip those. We're always in the one matching the last
00437       //  blocked we skipped.
00438       PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
00439       // Note that we've seen a #else in this conditional.
00440       CondInfo.FoundElse = true;
00441 
00442       // If the #if block wasn't entered then enter the #else block now.
00443       if (!CondInfo.FoundNonSkip) {
00444         CondInfo.FoundNonSkip = true;
00445 
00446         // Scan until the eod token.
00447         CurPTHLexer->ParsingPreprocessorDirective = true;
00448         DiscardUntilEndOfDirective();
00449         CurPTHLexer->ParsingPreprocessorDirective = false;
00450 
00451         break;
00452       }
00453 
00454       // Otherwise skip this block.
00455       continue;
00456     }
00457 
00458     assert(K == tok::pp_elif);
00459     PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
00460 
00461     // If this is a #elif with a #else before it, report the error.
00462     if (CondInfo.FoundElse)
00463       Diag(Tok, diag::pp_err_elif_after_else);
00464 
00465     // If this is in a skipping block or if we're already handled this #if
00466     // block, don't bother parsing the condition.  We just skip this block.
00467     if (CondInfo.FoundNonSkip)
00468       continue;
00469 
00470     // Evaluate the condition of the #elif.
00471     IdentifierInfo *IfNDefMacro = 0;
00472     CurPTHLexer->ParsingPreprocessorDirective = true;
00473     bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
00474     CurPTHLexer->ParsingPreprocessorDirective = false;
00475 
00476     // If this condition is true, enter it!
00477     if (ShouldEnter) {
00478       CondInfo.FoundNonSkip = true;
00479       break;
00480     }
00481 
00482     // Otherwise, skip this block and go to the next one.
00483     continue;
00484   }
00485 }
00486 
00487 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
00488 /// return null on failure.  isAngled indicates whether the file reference is
00489 /// for system #include's or not (i.e. using <> instead of "").
00490 const FileEntry *Preprocessor::LookupFile(
00491     StringRef Filename,
00492     bool isAngled,
00493     const DirectoryLookup *FromDir,
00494     const DirectoryLookup *&CurDir,
00495     SmallVectorImpl<char> *SearchPath,
00496     SmallVectorImpl<char> *RelativePath,
00497     Module **SuggestedModule,
00498     bool SkipCache) {
00499   // If the header lookup mechanism may be relative to the current file, pass in
00500   // info about where the current file is.
00501   const FileEntry *CurFileEnt = 0;
00502   if (!FromDir) {
00503     FileID FID = getCurrentFileLexer()->getFileID();
00504     CurFileEnt = SourceMgr.getFileEntryForID(FID);
00505 
00506     // If there is no file entry associated with this file, it must be the
00507     // predefines buffer.  Any other file is not lexed with a normal lexer, so
00508     // it won't be scanned for preprocessor directives.   If we have the
00509     // predefines buffer, resolve #include references (which come from the
00510     // -include command line argument) as if they came from the main file, this
00511     // affects file lookup etc.
00512     if (CurFileEnt == 0) {
00513       FID = SourceMgr.getMainFileID();
00514       CurFileEnt = SourceMgr.getFileEntryForID(FID);
00515     }
00516   }
00517 
00518   // Do a standard file entry lookup.
00519   CurDir = CurDirLookup;
00520   const FileEntry *FE = HeaderInfo.LookupFile(
00521       Filename, isAngled, FromDir, CurDir, CurFileEnt,
00522       SearchPath, RelativePath, SuggestedModule, SkipCache);
00523   if (FE) return FE;
00524 
00525   // Otherwise, see if this is a subframework header.  If so, this is relative
00526   // to one of the headers on the #include stack.  Walk the list of the current
00527   // headers on the #include stack and pass them to HeaderInfo.
00528   // FIXME: SuggestedModule!
00529   if (IsFileLexer()) {
00530     if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
00531       if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
00532                                                     SearchPath, RelativePath)))
00533         return FE;
00534   }
00535 
00536   for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
00537     IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
00538     if (IsFileLexer(ISEntry)) {
00539       if ((CurFileEnt =
00540            SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
00541         if ((FE = HeaderInfo.LookupSubframeworkHeader(
00542                 Filename, CurFileEnt, SearchPath, RelativePath)))
00543           return FE;
00544     }
00545   }
00546 
00547   // Otherwise, we really couldn't find the file.
00548   return 0;
00549 }
00550 
00551 
00552 //===----------------------------------------------------------------------===//
00553 // Preprocessor Directive Handling.
00554 //===----------------------------------------------------------------------===//
00555 
00556 /// HandleDirective - This callback is invoked when the lexer sees a # token
00557 /// at the start of a line.  This consumes the directive, modifies the
00558 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
00559 /// read is the correct one.
00560 void Preprocessor::HandleDirective(Token &Result) {
00561   // FIXME: Traditional: # with whitespace before it not recognized by K&R?
00562 
00563   // We just parsed a # character at the start of a line, so we're in directive
00564   // mode.  Tell the lexer this so any newlines we see will be converted into an
00565   // EOD token (which terminates the directive).
00566   CurPPLexer->ParsingPreprocessorDirective = true;
00567 
00568   ++NumDirectives;
00569 
00570   // We are about to read a token.  For the multiple-include optimization FA to
00571   // work, we have to remember if we had read any tokens *before* this
00572   // pp-directive.
00573   bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
00574 
00575   // Save the '#' token in case we need to return it later.
00576   Token SavedHash = Result;
00577 
00578   // Read the next token, the directive flavor.  This isn't expanded due to
00579   // C99 6.10.3p8.
00580   LexUnexpandedToken(Result);
00581 
00582   // C99 6.10.3p11: Is this preprocessor directive in macro invocation?  e.g.:
00583   //   #define A(x) #x
00584   //   A(abc
00585   //     #warning blah
00586   //   def)
00587   // If so, the user is relying on undefined behavior, emit a diagnostic. Do
00588   // not support this for #include-like directives, since that can result in
00589   // terrible diagnostics, and does not work in GCC.
00590   if (InMacroArgs) {
00591     if (IdentifierInfo *II = Result.getIdentifierInfo()) {
00592       switch (II->getPPKeywordID()) {
00593       case tok::pp_include:
00594       case tok::pp_import:
00595       case tok::pp_include_next:
00596       case tok::pp___include_macros:
00597         Diag(Result, diag::err_embedded_include) << II->getName();
00598         DiscardUntilEndOfDirective();
00599         return;
00600       default:
00601         break;
00602       }
00603     }
00604     Diag(Result, diag::ext_embedded_directive);
00605   }
00606 
00607 TryAgain:
00608   switch (Result.getKind()) {
00609   case tok::eod:
00610     return;   // null directive.
00611   case tok::comment:
00612     // Handle stuff like "# /*foo*/ define X" in -E -C mode.
00613     LexUnexpandedToken(Result);
00614     goto TryAgain;
00615   case tok::code_completion:
00616     if (CodeComplete)
00617       CodeComplete->CodeCompleteDirective(
00618                                     CurPPLexer->getConditionalStackDepth() > 0);
00619     setCodeCompletionReached();
00620     return;
00621   case tok::numeric_constant:  // # 7  GNU line marker directive.
00622     if (getLangOpts().AsmPreprocessor)
00623       break;  // # 4 is not a preprocessor directive in .S files.
00624     return HandleDigitDirective(Result);
00625   default:
00626     IdentifierInfo *II = Result.getIdentifierInfo();
00627     if (II == 0) break;  // Not an identifier.
00628 
00629     // Ask what the preprocessor keyword ID is.
00630     switch (II->getPPKeywordID()) {
00631     default: break;
00632     // C99 6.10.1 - Conditional Inclusion.
00633     case tok::pp_if:
00634       return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
00635     case tok::pp_ifdef:
00636       return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
00637     case tok::pp_ifndef:
00638       return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
00639     case tok::pp_elif:
00640       return HandleElifDirective(Result);
00641     case tok::pp_else:
00642       return HandleElseDirective(Result);
00643     case tok::pp_endif:
00644       return HandleEndifDirective(Result);
00645 
00646     // C99 6.10.2 - Source File Inclusion.
00647     case tok::pp_include:
00648       // Handle #include.
00649       return HandleIncludeDirective(SavedHash.getLocation(), Result);
00650     case tok::pp___include_macros:
00651       // Handle -imacros.
00652       return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); 
00653 
00654     // C99 6.10.3 - Macro Replacement.
00655     case tok::pp_define:
00656       return HandleDefineDirective(Result);
00657     case tok::pp_undef:
00658       return HandleUndefDirective(Result);
00659 
00660     // C99 6.10.4 - Line Control.
00661     case tok::pp_line:
00662       return HandleLineDirective(Result);
00663 
00664     // C99 6.10.5 - Error Directive.
00665     case tok::pp_error:
00666       return HandleUserDiagnosticDirective(Result, false);
00667 
00668     // C99 6.10.6 - Pragma Directive.
00669     case tok::pp_pragma:
00670       return HandlePragmaDirective(PIK_HashPragma);
00671 
00672     // GNU Extensions.
00673     case tok::pp_import:
00674       return HandleImportDirective(SavedHash.getLocation(), Result);
00675     case tok::pp_include_next:
00676       return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
00677 
00678     case tok::pp_warning:
00679       Diag(Result, diag::ext_pp_warning_directive);
00680       return HandleUserDiagnosticDirective(Result, true);
00681     case tok::pp_ident:
00682       return HandleIdentSCCSDirective(Result);
00683     case tok::pp_sccs:
00684       return HandleIdentSCCSDirective(Result);
00685     case tok::pp_assert:
00686       //isExtension = true;  // FIXME: implement #assert
00687       break;
00688     case tok::pp_unassert:
00689       //isExtension = true;  // FIXME: implement #unassert
00690       break;
00691         
00692     case tok::pp___public_macro:
00693       if (getLangOpts().Modules)
00694         return HandleMacroPublicDirective(Result);
00695       break;
00696         
00697     case tok::pp___private_macro:
00698       if (getLangOpts().Modules)
00699         return HandleMacroPrivateDirective(Result);
00700       break;
00701     }
00702     break;
00703   }
00704 
00705   // If this is a .S file, treat unknown # directives as non-preprocessor
00706   // directives.  This is important because # may be a comment or introduce
00707   // various pseudo-ops.  Just return the # token and push back the following
00708   // token to be lexed next time.
00709   if (getLangOpts().AsmPreprocessor) {
00710     Token *Toks = new Token[2];
00711     // Return the # and the token after it.
00712     Toks[0] = SavedHash;
00713     Toks[1] = Result;
00714     
00715     // If the second token is a hashhash token, then we need to translate it to
00716     // unknown so the token lexer doesn't try to perform token pasting.
00717     if (Result.is(tok::hashhash))
00718       Toks[1].setKind(tok::unknown);
00719     
00720     // Enter this token stream so that we re-lex the tokens.  Make sure to
00721     // enable macro expansion, in case the token after the # is an identifier
00722     // that is expanded.
00723     EnterTokenStream(Toks, 2, false, true);
00724     return;
00725   }
00726 
00727   // If we reached here, the preprocessing token is not valid!
00728   Diag(Result, diag::err_pp_invalid_directive);
00729 
00730   // Read the rest of the PP line.
00731   DiscardUntilEndOfDirective();
00732 
00733   // Okay, we're done parsing the directive.
00734 }
00735 
00736 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
00737 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
00738 static bool GetLineValue(Token &DigitTok, unsigned &Val,
00739                          unsigned DiagID, Preprocessor &PP) {
00740   if (DigitTok.isNot(tok::numeric_constant)) {
00741     PP.Diag(DigitTok, DiagID);
00742 
00743     if (DigitTok.isNot(tok::eod))
00744       PP.DiscardUntilEndOfDirective();
00745     return true;
00746   }
00747 
00748   SmallString<64> IntegerBuffer;
00749   IntegerBuffer.resize(DigitTok.getLength());
00750   const char *DigitTokBegin = &IntegerBuffer[0];
00751   bool Invalid = false;
00752   unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
00753   if (Invalid)
00754     return true;
00755   
00756   // Verify that we have a simple digit-sequence, and compute the value.  This
00757   // is always a simple digit string computed in decimal, so we do this manually
00758   // here.
00759   Val = 0;
00760   for (unsigned i = 0; i != ActualLength; ++i) {
00761     if (!isdigit(DigitTokBegin[i])) {
00762       PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
00763               diag::err_pp_line_digit_sequence);
00764       PP.DiscardUntilEndOfDirective();
00765       return true;
00766     }
00767 
00768     unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
00769     if (NextVal < Val) { // overflow.
00770       PP.Diag(DigitTok, DiagID);
00771       PP.DiscardUntilEndOfDirective();
00772       return true;
00773     }
00774     Val = NextVal;
00775   }
00776 
00777   // Reject 0, this is needed both by #line numbers and flags.
00778   if (Val == 0) {
00779     PP.Diag(DigitTok, DiagID);
00780     PP.DiscardUntilEndOfDirective();
00781     return true;
00782   }
00783 
00784   if (DigitTokBegin[0] == '0')
00785     PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal);
00786 
00787   return false;
00788 }
00789 
00790 /// HandleLineDirective - Handle #line directive: C99 6.10.4.  The two
00791 /// acceptable forms are:
00792 ///   # line digit-sequence
00793 ///   # line digit-sequence "s-char-sequence"
00794 void Preprocessor::HandleLineDirective(Token &Tok) {
00795   // Read the line # and string argument.  Per C99 6.10.4p5, these tokens are
00796   // expanded.
00797   Token DigitTok;
00798   Lex(DigitTok);
00799 
00800   // Validate the number and convert it to an unsigned.
00801   unsigned LineNo;
00802   if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
00803     return;
00804 
00805   // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
00806   // number greater than 2147483647".  C90 requires that the line # be <= 32767.
00807   unsigned LineLimit = 32768U;
00808   if (LangOpts.C99 || LangOpts.CPlusPlus0x)
00809     LineLimit = 2147483648U;
00810   if (LineNo >= LineLimit)
00811     Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
00812   else if (LangOpts.CPlusPlus0x && LineNo >= 32768U)
00813     Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
00814 
00815   int FilenameID = -1;
00816   Token StrTok;
00817   Lex(StrTok);
00818 
00819   // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
00820   // string followed by eod.
00821   if (StrTok.is(tok::eod))
00822     ; // ok
00823   else if (StrTok.isNot(tok::string_literal)) {
00824     Diag(StrTok, diag::err_pp_line_invalid_filename);
00825     return DiscardUntilEndOfDirective();
00826   } else if (StrTok.hasUDSuffix()) {
00827     Diag(StrTok, diag::err_invalid_string_udl);
00828     return DiscardUntilEndOfDirective();
00829   } else {
00830     // Parse and validate the string, converting it into a unique ID.
00831     StringLiteralParser Literal(&StrTok, 1, *this);
00832     assert(Literal.isAscii() && "Didn't allow wide strings in");
00833     if (Literal.hadError)
00834       return DiscardUntilEndOfDirective();
00835     if (Literal.Pascal) {
00836       Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
00837       return DiscardUntilEndOfDirective();
00838     }
00839     FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
00840 
00841     // Verify that there is nothing after the string, other than EOD.  Because
00842     // of C99 6.10.4p5, macros that expand to empty tokens are ok.
00843     CheckEndOfDirective("line", true);
00844   }
00845 
00846   SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
00847 
00848   if (Callbacks)
00849     Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
00850                            PPCallbacks::RenameFile,
00851                            SrcMgr::C_User);
00852 }
00853 
00854 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
00855 /// marker directive.
00856 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
00857                                 bool &IsSystemHeader, bool &IsExternCHeader,
00858                                 Preprocessor &PP) {
00859   unsigned FlagVal;
00860   Token FlagTok;
00861   PP.Lex(FlagTok);
00862   if (FlagTok.is(tok::eod)) return false;
00863   if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
00864     return true;
00865 
00866   if (FlagVal == 1) {
00867     IsFileEntry = true;
00868 
00869     PP.Lex(FlagTok);
00870     if (FlagTok.is(tok::eod)) return false;
00871     if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
00872       return true;
00873   } else if (FlagVal == 2) {
00874     IsFileExit = true;
00875 
00876     SourceManager &SM = PP.getSourceManager();
00877     // If we are leaving the current presumed file, check to make sure the
00878     // presumed include stack isn't empty!
00879     FileID CurFileID =
00880       SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
00881     PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
00882     if (PLoc.isInvalid())
00883       return true;
00884     
00885     // If there is no include loc (main file) or if the include loc is in a
00886     // different physical file, then we aren't in a "1" line marker flag region.
00887     SourceLocation IncLoc = PLoc.getIncludeLoc();
00888     if (IncLoc.isInvalid() ||
00889         SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
00890       PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
00891       PP.DiscardUntilEndOfDirective();
00892       return true;
00893     }
00894 
00895     PP.Lex(FlagTok);
00896     if (FlagTok.is(tok::eod)) return false;
00897     if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
00898       return true;
00899   }
00900 
00901   // We must have 3 if there are still flags.
00902   if (FlagVal != 3) {
00903     PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
00904     PP.DiscardUntilEndOfDirective();
00905     return true;
00906   }
00907 
00908   IsSystemHeader = true;
00909 
00910   PP.Lex(FlagTok);
00911   if (FlagTok.is(tok::eod)) return false;
00912   if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
00913     return true;
00914 
00915   // We must have 4 if there is yet another flag.
00916   if (FlagVal != 4) {
00917     PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
00918     PP.DiscardUntilEndOfDirective();
00919     return true;
00920   }
00921 
00922   IsExternCHeader = true;
00923 
00924   PP.Lex(FlagTok);
00925   if (FlagTok.is(tok::eod)) return false;
00926 
00927   // There are no more valid flags here.
00928   PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
00929   PP.DiscardUntilEndOfDirective();
00930   return true;
00931 }
00932 
00933 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
00934 /// one of the following forms:
00935 ///
00936 ///     # 42
00937 ///     # 42 "file" ('1' | '2')?
00938 ///     # 42 "file" ('1' | '2')? '3' '4'?
00939 ///
00940 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
00941   // Validate the number and convert it to an unsigned.  GNU does not have a
00942   // line # limit other than it fit in 32-bits.
00943   unsigned LineNo;
00944   if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
00945                    *this))
00946     return;
00947 
00948   Token StrTok;
00949   Lex(StrTok);
00950 
00951   bool IsFileEntry = false, IsFileExit = false;
00952   bool IsSystemHeader = false, IsExternCHeader = false;
00953   int FilenameID = -1;
00954 
00955   // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
00956   // string followed by eod.
00957   if (StrTok.is(tok::eod))
00958     ; // ok
00959   else if (StrTok.isNot(tok::string_literal)) {
00960     Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
00961     return DiscardUntilEndOfDirective();
00962   } else if (StrTok.hasUDSuffix()) {
00963     Diag(StrTok, diag::err_invalid_string_udl);
00964     return DiscardUntilEndOfDirective();
00965   } else {
00966     // Parse and validate the string, converting it into a unique ID.
00967     StringLiteralParser Literal(&StrTok, 1, *this);
00968     assert(Literal.isAscii() && "Didn't allow wide strings in");
00969     if (Literal.hadError)
00970       return DiscardUntilEndOfDirective();
00971     if (Literal.Pascal) {
00972       Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
00973       return DiscardUntilEndOfDirective();
00974     }
00975     FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
00976 
00977     // If a filename was present, read any flags that are present.
00978     if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
00979                             IsSystemHeader, IsExternCHeader, *this))
00980       return;
00981   }
00982 
00983   // Create a line note with this information.
00984   SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
00985                         IsFileEntry, IsFileExit,
00986                         IsSystemHeader, IsExternCHeader);
00987 
00988   // If the preprocessor has callbacks installed, notify them of the #line
00989   // change.  This is used so that the line marker comes out in -E mode for
00990   // example.
00991   if (Callbacks) {
00992     PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
00993     if (IsFileEntry)
00994       Reason = PPCallbacks::EnterFile;
00995     else if (IsFileExit)
00996       Reason = PPCallbacks::ExitFile;
00997     SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
00998     if (IsExternCHeader)
00999       FileKind = SrcMgr::C_ExternCSystem;
01000     else if (IsSystemHeader)
01001       FileKind = SrcMgr::C_System;
01002 
01003     Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
01004   }
01005 }
01006 
01007 
01008 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
01009 ///
01010 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
01011                                                  bool isWarning) {
01012   // PTH doesn't emit #warning or #error directives.
01013   if (CurPTHLexer)
01014     return CurPTHLexer->DiscardToEndOfLine();
01015 
01016   // Read the rest of the line raw.  We do this because we don't want macros
01017   // to be expanded and we don't require that the tokens be valid preprocessing
01018   // tokens.  For example, this is allowed: "#warning `   'foo".  GCC does
01019   // collapse multiple consequtive white space between tokens, but this isn't
01020   // specified by the standard.
01021   SmallString<128> Message;
01022   CurLexer->ReadToEndOfLine(&Message);
01023 
01024   // Find the first non-whitespace character, so that we can make the
01025   // diagnostic more succinct.
01026   StringRef Msg = Message.str().ltrim(" ");
01027 
01028   if (isWarning)
01029     Diag(Tok, diag::pp_hash_warning) << Msg;
01030   else
01031     Diag(Tok, diag::err_pp_hash_error) << Msg;
01032 }
01033 
01034 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
01035 ///
01036 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
01037   // Yes, this directive is an extension.
01038   Diag(Tok, diag::ext_pp_ident_directive);
01039 
01040   // Read the string argument.
01041   Token StrTok;
01042   Lex(StrTok);
01043 
01044   // If the token kind isn't a string, it's a malformed directive.
01045   if (StrTok.isNot(tok::string_literal) &&
01046       StrTok.isNot(tok::wide_string_literal)) {
01047     Diag(StrTok, diag::err_pp_malformed_ident);
01048     if (StrTok.isNot(tok::eod))
01049       DiscardUntilEndOfDirective();
01050     return;
01051   }
01052 
01053   if (StrTok.hasUDSuffix()) {
01054     Diag(StrTok, diag::err_invalid_string_udl);
01055     return DiscardUntilEndOfDirective();
01056   }
01057 
01058   // Verify that there is nothing after the string, other than EOD.
01059   CheckEndOfDirective("ident");
01060 
01061   if (Callbacks) {
01062     bool Invalid = false;
01063     std::string Str = getSpelling(StrTok, &Invalid);
01064     if (!Invalid)
01065       Callbacks->Ident(Tok.getLocation(), Str);
01066   }
01067 }
01068 
01069 /// \brief Handle a #public directive.
01070 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
01071   Token MacroNameTok;
01072   ReadMacroName(MacroNameTok, 2);
01073   
01074   // Error reading macro name?  If so, diagnostic already issued.
01075   if (MacroNameTok.is(tok::eod))
01076     return;
01077 
01078   // Check to see if this is the last token on the #__public_macro line.
01079   CheckEndOfDirective("__public_macro");
01080 
01081   // Okay, we finally have a valid identifier to undef.
01082   MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
01083   
01084   // If the macro is not defined, this is an error.
01085   if (MI == 0) {
01086     Diag(MacroNameTok, diag::err_pp_visibility_non_macro)
01087       << MacroNameTok.getIdentifierInfo();
01088     return;
01089   }
01090   
01091   // Note that this macro has now been exported.
01092   MI->setVisibility(/*IsPublic=*/true, MacroNameTok.getLocation());
01093   
01094   // If this macro definition came from a PCH file, mark it
01095   // as having changed since serialization.
01096   if (MI->isFromAST())
01097     MI->setChangedAfterLoad();
01098 }
01099 
01100 /// \brief Handle a #private directive.
01101 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
01102   Token MacroNameTok;
01103   ReadMacroName(MacroNameTok, 2);
01104   
01105   // Error reading macro name?  If so, diagnostic already issued.
01106   if (MacroNameTok.is(tok::eod))
01107     return;
01108   
01109   // Check to see if this is the last token on the #__private_macro line.
01110   CheckEndOfDirective("__private_macro");
01111   
01112   // Okay, we finally have a valid identifier to undef.
01113   MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
01114   
01115   // If the macro is not defined, this is an error.
01116   if (MI == 0) {
01117     Diag(MacroNameTok, diag::err_pp_visibility_non_macro)
01118       << MacroNameTok.getIdentifierInfo();
01119     return;
01120   }
01121   
01122   // Note that this macro has now been marked private.
01123   MI->setVisibility(/*IsPublic=*/false, MacroNameTok.getLocation());
01124   
01125   // If this macro definition came from a PCH file, mark it
01126   // as having changed since serialization.
01127   if (MI->isFromAST())
01128     MI->setChangedAfterLoad();
01129 }
01130 
01131 //===----------------------------------------------------------------------===//
01132 // Preprocessor Include Directive Handling.
01133 //===----------------------------------------------------------------------===//
01134 
01135 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
01136 /// checked and spelled filename, e.g. as an operand of #include. This returns
01137 /// true if the input filename was in <>'s or false if it were in ""'s.  The
01138 /// caller is expected to provide a buffer that is large enough to hold the
01139 /// spelling of the filename, but is also expected to handle the case when
01140 /// this method decides to use a different buffer.
01141 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
01142                                               StringRef &Buffer) {
01143   // Get the text form of the filename.
01144   assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
01145 
01146   // Make sure the filename is <x> or "x".
01147   bool isAngled;
01148   if (Buffer[0] == '<') {
01149     if (Buffer.back() != '>') {
01150       Diag(Loc, diag::err_pp_expects_filename);
01151       Buffer = StringRef();
01152       return true;
01153     }
01154     isAngled = true;
01155   } else if (Buffer[0] == '"') {
01156     if (Buffer.back() != '"') {
01157       Diag(Loc, diag::err_pp_expects_filename);
01158       Buffer = StringRef();
01159       return true;
01160     }
01161     isAngled = false;
01162   } else {
01163     Diag(Loc, diag::err_pp_expects_filename);
01164     Buffer = StringRef();
01165     return true;
01166   }
01167 
01168   // Diagnose #include "" as invalid.
01169   if (Buffer.size() <= 2) {
01170     Diag(Loc, diag::err_pp_empty_filename);
01171     Buffer = StringRef();
01172     return true;
01173   }
01174 
01175   // Skip the brackets.
01176   Buffer = Buffer.substr(1, Buffer.size()-2);
01177   return isAngled;
01178 }
01179 
01180 /// ConcatenateIncludeName - Handle cases where the #include name is expanded
01181 /// from a macro as multiple tokens, which need to be glued together.  This
01182 /// occurs for code like:
01183 ///    #define FOO <a/b.h>
01184 ///    #include FOO
01185 /// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
01186 ///
01187 /// This code concatenates and consumes tokens up to the '>' token.  It returns
01188 /// false if the > was found, otherwise it returns true if it finds and consumes
01189 /// the EOD marker.
01190 bool Preprocessor::ConcatenateIncludeName(
01191                                         SmallString<128> &FilenameBuffer,
01192                                           SourceLocation &End) {
01193   Token CurTok;
01194 
01195   Lex(CurTok);
01196   while (CurTok.isNot(tok::eod)) {
01197     End = CurTok.getLocation();
01198     
01199     // FIXME: Provide code completion for #includes.
01200     if (CurTok.is(tok::code_completion)) {
01201       setCodeCompletionReached();
01202       Lex(CurTok);
01203       continue;
01204     }
01205 
01206     // Append the spelling of this token to the buffer. If there was a space
01207     // before it, add it now.
01208     if (CurTok.hasLeadingSpace())
01209       FilenameBuffer.push_back(' ');
01210 
01211     // Get the spelling of the token, directly into FilenameBuffer if possible.
01212     unsigned PreAppendSize = FilenameBuffer.size();
01213     FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
01214 
01215     const char *BufPtr = &FilenameBuffer[PreAppendSize];
01216     unsigned ActualLen = getSpelling(CurTok, BufPtr);
01217 
01218     // If the token was spelled somewhere else, copy it into FilenameBuffer.
01219     if (BufPtr != &FilenameBuffer[PreAppendSize])
01220       memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
01221 
01222     // Resize FilenameBuffer to the correct size.
01223     if (CurTok.getLength() != ActualLen)
01224       FilenameBuffer.resize(PreAppendSize+ActualLen);
01225 
01226     // If we found the '>' marker, return success.
01227     if (CurTok.is(tok::greater))
01228       return false;
01229 
01230     Lex(CurTok);
01231   }
01232 
01233   // If we hit the eod marker, emit an error and return true so that the caller
01234   // knows the EOD has been read.
01235   Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
01236   return true;
01237 }
01238 
01239 /// HandleIncludeDirective - The "#include" tokens have just been read, read the
01240 /// file to be included from the lexer, then include it!  This is a common
01241 /// routine with functionality shared between #include, #include_next and
01242 /// #import.  LookupFrom is set when this is a #include_next directive, it
01243 /// specifies the file to start searching from.
01244 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, 
01245                                           Token &IncludeTok,
01246                                           const DirectoryLookup *LookupFrom,
01247                                           bool isImport) {
01248 
01249   Token FilenameTok;
01250   CurPPLexer->LexIncludeFilename(FilenameTok);
01251 
01252   // Reserve a buffer to get the spelling.
01253   SmallString<128> FilenameBuffer;
01254   StringRef Filename;
01255   SourceLocation End;
01256   SourceLocation CharEnd; // the end of this directive, in characters
01257   
01258   switch (FilenameTok.getKind()) {
01259   case tok::eod:
01260     // If the token kind is EOD, the error has already been diagnosed.
01261     return;
01262 
01263   case tok::angle_string_literal:
01264   case tok::string_literal:
01265     Filename = getSpelling(FilenameTok, FilenameBuffer);
01266     End = FilenameTok.getLocation();
01267     CharEnd = End.getLocWithOffset(Filename.size());
01268     break;
01269 
01270   case tok::less:
01271     // This could be a <foo/bar.h> file coming from a macro expansion.  In this
01272     // case, glue the tokens together into FilenameBuffer and interpret those.
01273     FilenameBuffer.push_back('<');
01274     if (ConcatenateIncludeName(FilenameBuffer, End))
01275       return;   // Found <eod> but no ">"?  Diagnostic already emitted.
01276     Filename = FilenameBuffer.str();
01277     CharEnd = getLocForEndOfToken(End);
01278     break;
01279   default:
01280     Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
01281     DiscardUntilEndOfDirective();
01282     return;
01283   }
01284 
01285   StringRef OriginalFilename = Filename;
01286   bool isAngled =
01287     GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
01288   // If GetIncludeFilenameSpelling set the start ptr to null, there was an
01289   // error.
01290   if (Filename.empty()) {
01291     DiscardUntilEndOfDirective();
01292     return;
01293   }
01294 
01295   // Verify that there is nothing after the filename, other than EOD.  Note that
01296   // we allow macros that expand to nothing after the filename, because this
01297   // falls into the category of "#include pp-tokens new-line" specified in
01298   // C99 6.10.2p4.
01299   CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
01300 
01301   // Check that we don't have infinite #include recursion.
01302   if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
01303     Diag(FilenameTok, diag::err_pp_include_too_deep);
01304     return;
01305   }
01306 
01307   // Complain about attempts to #include files in an audit pragma.
01308   if (PragmaARCCFCodeAuditedLoc.isValid()) {
01309     Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
01310     Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
01311 
01312     // Immediately leave the pragma.
01313     PragmaARCCFCodeAuditedLoc = SourceLocation();
01314   }
01315 
01316   if (HeaderInfo.HasIncludeAliasMap()) {
01317     // Map the filename with the brackets still attached.  If the name doesn't 
01318     // map to anything, fall back on the filename we've already gotten the 
01319     // spelling for.
01320     StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
01321     if (!NewName.empty())
01322       Filename = NewName;
01323   }
01324 
01325   // Search include directories.
01326   const DirectoryLookup *CurDir;
01327   SmallString<1024> SearchPath;
01328   SmallString<1024> RelativePath;
01329   // We get the raw path only if we have 'Callbacks' to which we later pass
01330   // the path.
01331   Module *SuggestedModule = 0;
01332   const FileEntry *File = LookupFile(
01333       Filename, isAngled, LookupFrom, CurDir,
01334       Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL,
01335       getLangOpts().Modules? &SuggestedModule : 0);
01336 
01337   if (Callbacks) {
01338     if (!File) {
01339       // Give the clients a chance to recover.
01340       SmallString<128> RecoveryPath;
01341       if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
01342         if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
01343           // Add the recovery path to the list of search paths.
01344           DirectoryLookup DL(DE, SrcMgr::C_User, true, false);
01345           HeaderInfo.AddSearchPath(DL, isAngled);
01346           
01347           // Try the lookup again, skipping the cache.
01348           File = LookupFile(Filename, isAngled, LookupFrom, CurDir, 0, 0,
01349                             getLangOpts().Modules? &SuggestedModule : 0,
01350                             /*SkipCache*/true);
01351         }
01352       }
01353     }
01354     
01355     // Notify the callback object that we've seen an inclusion directive.
01356     Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, File,
01357                                   End, SearchPath, RelativePath);
01358   }
01359   
01360   if (File == 0) {
01361     if (!SuppressIncludeNotFoundError)
01362       Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
01363     return;
01364   }
01365 
01366   // If we are supposed to import a module rather than including the header,
01367   // do so now.
01368   if (SuggestedModule) {
01369     // Compute the module access path corresponding to this module.
01370     // FIXME: Should we have a second loadModule() overload to avoid this
01371     // extra lookup step?
01372     llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
01373     for (Module *Mod = SuggestedModule; Mod; Mod = Mod->Parent)
01374       Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
01375                                     FilenameTok.getLocation()));
01376     std::reverse(Path.begin(), Path.end());
01377 
01378     // Warn that we're replacing the include/import with a module import.
01379     SmallString<128> PathString;
01380     for (unsigned I = 0, N = Path.size(); I != N; ++I) {
01381       if (I)
01382         PathString += '.';
01383       PathString += Path[I].first->getName();
01384     }
01385     int IncludeKind = 0;
01386     
01387     switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
01388     case tok::pp_include:
01389       IncludeKind = 0;
01390       break;
01391       
01392     case tok::pp_import:
01393       IncludeKind = 1;
01394       break;        
01395         
01396     case tok::pp_include_next:
01397       IncludeKind = 2;
01398       break;
01399         
01400     case tok::pp___include_macros:
01401       IncludeKind = 3;
01402       break;
01403         
01404     default:
01405       llvm_unreachable("unknown include directive kind");
01406     }
01407 
01408     // Determine whether we are actually building the module that this
01409     // include directive maps to.
01410     bool BuildingImportedModule
01411       = Path[0].first->getName() == getLangOpts().CurrentModule;
01412     
01413     if (!BuildingImportedModule && getLangOpts().ObjC2) {
01414       // If we're not building the imported module, warn that we're going
01415       // to automatically turn this inclusion directive into a module import.
01416       // We only do this in Objective-C, where we have a module-import syntax.
01417       CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd), 
01418                                    /*IsTokenRange=*/false);
01419       Diag(HashLoc, diag::warn_auto_module_import)
01420         << IncludeKind << PathString 
01421         << FixItHint::CreateReplacement(ReplaceRange,
01422              "@__experimental_modules_import " + PathString.str().str() + ";");
01423     }
01424     
01425     // Load the module.
01426     // If this was an #__include_macros directive, only make macros visible.
01427     Module::NameVisibilityKind Visibility 
01428       = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible;
01429     Module *Imported
01430       = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
01431                                    /*IsIncludeDirective=*/true);
01432     
01433     // If this header isn't part of the module we're building, we're done.
01434     if (!BuildingImportedModule && Imported)
01435       return;
01436   }
01437   
01438   // The #included file will be considered to be a system header if either it is
01439   // in a system include directory, or if the #includer is a system include
01440   // header.
01441   SrcMgr::CharacteristicKind FileCharacter =
01442     std::max(HeaderInfo.getFileDirFlavor(File),
01443              SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
01444 
01445   // Ask HeaderInfo if we should enter this #include file.  If not, #including
01446   // this file will have no effect.
01447   if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
01448     if (Callbacks)
01449       Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
01450     return;
01451   }
01452 
01453   // Look up the file, create a File ID for it.
01454   SourceLocation IncludePos = End;
01455   // If the filename string was the result of macro expansions, set the include
01456   // position on the file where it will be included and after the expansions.
01457   if (IncludePos.isMacroID())
01458     IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
01459   FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
01460   assert(!FID.isInvalid() && "Expected valid file ID");
01461 
01462   // Finally, if all is good, enter the new file!
01463   EnterSourceFile(FID, CurDir, FilenameTok.getLocation());
01464 }
01465 
01466 /// HandleIncludeNextDirective - Implements #include_next.
01467 ///
01468 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
01469                                               Token &IncludeNextTok) {
01470   Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
01471 
01472   // #include_next is like #include, except that we start searching after
01473   // the current found directory.  If we can't do this, issue a
01474   // diagnostic.
01475   const DirectoryLookup *Lookup = CurDirLookup;
01476   if (isInPrimaryFile()) {
01477     Lookup = 0;
01478     Diag(IncludeNextTok, diag::pp_include_next_in_primary);
01479   } else if (Lookup == 0) {
01480     Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
01481   } else {
01482     // Start looking up in the next directory.
01483     ++Lookup;
01484   }
01485 
01486   return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
01487 }
01488 
01489 /// HandleMicrosoftImportDirective - Implements #import for Microsoft Mode
01490 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
01491   // The Microsoft #import directive takes a type library and generates header
01492   // files from it, and includes those.  This is beyond the scope of what clang
01493   // does, so we ignore it and error out.  However, #import can optionally have
01494   // trailing attributes that span multiple lines.  We're going to eat those
01495   // so we can continue processing from there.
01496   Diag(Tok, diag::err_pp_import_directive_ms );
01497 
01498   // Read tokens until we get to the end of the directive.  Note that the 
01499   // directive can be split over multiple lines using the backslash character.
01500   DiscardUntilEndOfDirective();
01501 }
01502 
01503 /// HandleImportDirective - Implements #import.
01504 ///
01505 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
01506                                          Token &ImportTok) {
01507   if (!LangOpts.ObjC1) {  // #import is standard for ObjC.
01508     if (LangOpts.MicrosoftMode)
01509       return HandleMicrosoftImportDirective(ImportTok);
01510     Diag(ImportTok, diag::ext_pp_import_directive);
01511   }
01512   return HandleIncludeDirective(HashLoc, ImportTok, 0, true);
01513 }
01514 
01515 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
01516 /// pseudo directive in the predefines buffer.  This handles it by sucking all
01517 /// tokens through the preprocessor and discarding them (only keeping the side
01518 /// effects on the preprocessor).
01519 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
01520                                                 Token &IncludeMacrosTok) {
01521   // This directive should only occur in the predefines buffer.  If not, emit an
01522   // error and reject it.
01523   SourceLocation Loc = IncludeMacrosTok.getLocation();
01524   if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
01525     Diag(IncludeMacrosTok.getLocation(),
01526          diag::pp_include_macros_out_of_predefines);
01527     DiscardUntilEndOfDirective();
01528     return;
01529   }
01530 
01531   // Treat this as a normal #include for checking purposes.  If this is
01532   // successful, it will push a new lexer onto the include stack.
01533   HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false);
01534 
01535   Token TmpTok;
01536   do {
01537     Lex(TmpTok);
01538     assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
01539   } while (TmpTok.isNot(tok::hashhash));
01540 }
01541 
01542 //===----------------------------------------------------------------------===//
01543 // Preprocessor Macro Directive Handling.
01544 //===----------------------------------------------------------------------===//
01545 
01546 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
01547 /// definition has just been read.  Lex the rest of the arguments and the
01548 /// closing ), updating MI with what we learn.  Return true if an error occurs
01549 /// parsing the arg list.
01550 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
01551   SmallVector<IdentifierInfo*, 32> Arguments;
01552 
01553   while (1) {
01554     LexUnexpandedToken(Tok);
01555     switch (Tok.getKind()) {
01556     case tok::r_paren:
01557       // Found the end of the argument list.
01558       if (Arguments.empty())  // #define FOO()
01559         return false;
01560       // Otherwise we have #define FOO(A,)
01561       Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
01562       return true;
01563     case tok::ellipsis:  // #define X(... -> C99 varargs
01564       if (!LangOpts.C99)
01565         Diag(Tok, LangOpts.CPlusPlus0x ? 
01566              diag::warn_cxx98_compat_variadic_macro :
01567              diag::ext_variadic_macro);
01568 
01569       // Lex the token after the identifier.
01570       LexUnexpandedToken(Tok);
01571       if (Tok.isNot(tok::r_paren)) {
01572         Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
01573         return true;
01574       }
01575       // Add the __VA_ARGS__ identifier as an argument.
01576       Arguments.push_back(Ident__VA_ARGS__);
01577       MI->setIsC99Varargs();
01578       MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
01579       return false;
01580     case tok::eod:  // #define X(
01581       Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
01582       return true;
01583     default:
01584       // Handle keywords and identifiers here to accept things like
01585       // #define Foo(for) for.
01586       IdentifierInfo *II = Tok.getIdentifierInfo();
01587       if (II == 0) {
01588         // #define X(1
01589         Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
01590         return true;
01591       }
01592 
01593       // If this is already used as an argument, it is used multiple times (e.g.
01594       // #define X(A,A.
01595       if (std::find(Arguments.begin(), Arguments.end(), II) !=
01596           Arguments.end()) {  // C99 6.10.3p6
01597         Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
01598         return true;
01599       }
01600 
01601       // Add the argument to the macro info.
01602       Arguments.push_back(II);
01603 
01604       // Lex the token after the identifier.
01605       LexUnexpandedToken(Tok);
01606 
01607       switch (Tok.getKind()) {
01608       default:          // #define X(A B
01609         Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
01610         return true;
01611       case tok::r_paren: // #define X(A)
01612         MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
01613         return false;
01614       case tok::comma:  // #define X(A,
01615         break;
01616       case tok::ellipsis:  // #define X(A... -> GCC extension
01617         // Diagnose extension.
01618         Diag(Tok, diag::ext_named_variadic_macro);
01619 
01620         // Lex the token after the identifier.
01621         LexUnexpandedToken(Tok);
01622         if (Tok.isNot(tok::r_paren)) {
01623           Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
01624           return true;
01625         }
01626 
01627         MI->setIsGNUVarargs();
01628         MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
01629         return false;
01630       }
01631     }
01632   }
01633 }
01634 
01635 /// HandleDefineDirective - Implements #define.  This consumes the entire macro
01636 /// line then lets the caller lex the next real token.
01637 void Preprocessor::HandleDefineDirective(Token &DefineTok) {
01638   ++NumDefined;
01639 
01640   Token MacroNameTok;
01641   ReadMacroName(MacroNameTok, 1);
01642 
01643   // Error reading macro name?  If so, diagnostic already issued.
01644   if (MacroNameTok.is(tok::eod))
01645     return;
01646 
01647   Token LastTok = MacroNameTok;
01648 
01649   // If we are supposed to keep comments in #defines, reenable comment saving
01650   // mode.
01651   if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
01652 
01653   // Create the new macro.
01654   MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
01655 
01656   Token Tok;
01657   LexUnexpandedToken(Tok);
01658 
01659   // If this is a function-like macro definition, parse the argument list,
01660   // marking each of the identifiers as being used as macro arguments.  Also,
01661   // check other constraints on the first token of the macro body.
01662   if (Tok.is(tok::eod)) {
01663     // If there is no body to this macro, we have no special handling here.
01664   } else if (Tok.hasLeadingSpace()) {
01665     // This is a normal token with leading space.  Clear the leading space
01666     // marker on the first token to get proper expansion.
01667     Tok.clearFlag(Token::LeadingSpace);
01668   } else if (Tok.is(tok::l_paren)) {
01669     // This is a function-like macro definition.  Read the argument list.
01670     MI->setIsFunctionLike();
01671     if (ReadMacroDefinitionArgList(MI, LastTok)) {
01672       // Forget about MI.
01673       ReleaseMacroInfo(MI);
01674       // Throw away the rest of the line.
01675       if (CurPPLexer->ParsingPreprocessorDirective)
01676         DiscardUntilEndOfDirective();
01677       return;
01678     }
01679 
01680     // If this is a definition of a variadic C99 function-like macro, not using
01681     // the GNU named varargs extension, enabled __VA_ARGS__.
01682 
01683     // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
01684     // This gets unpoisoned where it is allowed.
01685     assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
01686     if (MI->isC99Varargs())
01687       Ident__VA_ARGS__->setIsPoisoned(false);
01688 
01689     // Read the first token after the arg list for down below.
01690     LexUnexpandedToken(Tok);
01691   } else if (LangOpts.C99 || LangOpts.CPlusPlus0x) {
01692     // C99 requires whitespace between the macro definition and the body.  Emit
01693     // a diagnostic for something like "#define X+".
01694     Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
01695   } else {
01696     // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
01697     // first character of a replacement list is not a character required by
01698     // subclause 5.2.1, then there shall be white-space separation between the
01699     // identifier and the replacement list.".  5.2.1 lists this set:
01700     //   "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
01701     // is irrelevant here.
01702     bool isInvalid = false;
01703     if (Tok.is(tok::at)) // @ is not in the list above.
01704       isInvalid = true;
01705     else if (Tok.is(tok::unknown)) {
01706       // If we have an unknown token, it is something strange like "`".  Since
01707       // all of valid characters would have lexed into a single character
01708       // token of some sort, we know this is not a valid case.
01709       isInvalid = true;
01710     }
01711     if (isInvalid)
01712       Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
01713     else
01714       Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
01715   }
01716 
01717   if (!Tok.is(tok::eod))
01718     LastTok = Tok;
01719 
01720   // Read the rest of the macro body.
01721   if (MI->isObjectLike()) {
01722     // Object-like macros are very simple, just read their body.
01723     while (Tok.isNot(tok::eod)) {
01724       LastTok = Tok;
01725       MI->AddTokenToBody(Tok);
01726       // Get the next token of the macro.
01727       LexUnexpandedToken(Tok);
01728     }
01729 
01730   } else {
01731     // Otherwise, read the body of a function-like macro.  While we are at it,
01732     // check C99 6.10.3.2p1: ensure that # operators are followed by macro
01733     // parameters in function-like macro expansions.
01734     while (Tok.isNot(tok::eod)) {
01735       LastTok = Tok;
01736 
01737       if (Tok.isNot(tok::hash)) {
01738         MI->AddTokenToBody(Tok);
01739 
01740         // Get the next token of the macro.
01741         LexUnexpandedToken(Tok);
01742         continue;
01743       }
01744 
01745       // Get the next token of the macro.
01746       LexUnexpandedToken(Tok);
01747 
01748       // Check for a valid macro arg identifier.
01749       if (Tok.getIdentifierInfo() == 0 ||
01750           MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
01751 
01752         // If this is assembler-with-cpp mode, we accept random gibberish after
01753         // the '#' because '#' is often a comment character.  However, change
01754         // the kind of the token to tok::unknown so that the preprocessor isn't
01755         // confused.
01756         if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
01757           LastTok.setKind(tok::unknown);
01758         } else {
01759           Diag(Tok, diag::err_pp_stringize_not_parameter);
01760           ReleaseMacroInfo(MI);
01761 
01762           // Disable __VA_ARGS__ again.
01763           Ident__VA_ARGS__->setIsPoisoned(true);
01764           return;
01765         }
01766       }
01767 
01768       // Things look ok, add the '#' and param name tokens to the macro.
01769       MI->AddTokenToBody(LastTok);
01770       MI->AddTokenToBody(Tok);
01771       LastTok = Tok;
01772 
01773       // Get the next token of the macro.
01774       LexUnexpandedToken(Tok);
01775     }
01776   }
01777 
01778 
01779   // Disable __VA_ARGS__ again.
01780   Ident__VA_ARGS__->setIsPoisoned(true);
01781 
01782   // Check that there is no paste (##) operator at the beginning or end of the
01783   // replacement list.
01784   unsigned NumTokens = MI->getNumTokens();
01785   if (NumTokens != 0) {
01786     if (MI->getReplacementToken(0).is(tok::hashhash)) {
01787       Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
01788       ReleaseMacroInfo(MI);
01789       return;
01790     }
01791     if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
01792       Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
01793       ReleaseMacroInfo(MI);
01794       return;
01795     }
01796   }
01797 
01798   MI->setDefinitionEndLoc(LastTok.getLocation());
01799 
01800   // Finally, if this identifier already had a macro defined for it, verify that
01801   // the macro bodies are identical and free the old definition.
01802   if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
01803     // It is very common for system headers to have tons of macro redefinitions
01804     // and for warnings to be disabled in system headers.  If this is the case,
01805     // then don't bother calling MacroInfo::isIdenticalTo.
01806     if (!getDiagnostics().getSuppressSystemWarnings() ||
01807         !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
01808       if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
01809         Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
01810 
01811       // Macros must be identical.  This means all tokens and whitespace
01812       // separation must be the same.  C99 6.10.3.2.
01813       if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
01814           !MI->isIdenticalTo(*OtherMI, *this)) {
01815         Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
01816           << MacroNameTok.getIdentifierInfo();
01817         Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
01818       }
01819     }
01820     if (OtherMI->isWarnIfUnused())
01821       WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
01822     ReleaseMacroInfo(OtherMI);
01823   }
01824 
01825   setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
01826 
01827   assert(!MI->isUsed());
01828   // If we need warning for not using the macro, add its location in the
01829   // warn-because-unused-macro set. If it gets used it will be removed from set.
01830   if (isInPrimaryFile() && // don't warn for include'd macros.
01831       Diags->getDiagnosticLevel(diag::pp_macro_not_used,
01832           MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) {
01833     MI->setIsWarnIfUnused(true);
01834     WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
01835   }
01836 
01837   // If the callbacks want to know, tell them about the macro definition.
01838   if (Callbacks)
01839     Callbacks->MacroDefined(MacroNameTok, MI);
01840 }
01841 
01842 /// HandleUndefDirective - Implements #undef.
01843 ///
01844 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
01845   ++NumUndefined;
01846 
01847   Token MacroNameTok;
01848   ReadMacroName(MacroNameTok, 2);
01849 
01850   // Error reading macro name?  If so, diagnostic already issued.
01851   if (MacroNameTok.is(tok::eod))
01852     return;
01853 
01854   // Check to see if this is the last token on the #undef line.
01855   CheckEndOfDirective("undef");
01856 
01857   // Okay, we finally have a valid identifier to undef.
01858   MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
01859 
01860   // If the macro is not defined, this is a noop undef, just return.
01861   if (MI == 0) return;
01862 
01863   if (!MI->isUsed() && MI->isWarnIfUnused())
01864     Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
01865 
01866   // If the callbacks want to know, tell them about the macro #undef.
01867   if (Callbacks)
01868     Callbacks->MacroUndefined(MacroNameTok, MI);
01869 
01870   if (MI->isWarnIfUnused())
01871     WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
01872 
01873   // Free macro definition.
01874   ReleaseMacroInfo(MI);
01875   setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
01876 }
01877 
01878 
01879 //===----------------------------------------------------------------------===//
01880 // Preprocessor Conditional Directive Handling.
01881 //===----------------------------------------------------------------------===//
01882 
01883 /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive.  isIfndef is
01884 /// true when this is a #ifndef directive.  ReadAnyTokensBeforeDirective is true
01885 /// if any tokens have been returned or pp-directives activated before this
01886 /// #ifndef has been lexed.
01887 ///
01888 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
01889                                         bool ReadAnyTokensBeforeDirective) {
01890   ++NumIf;
01891   Token DirectiveTok = Result;
01892 
01893   Token MacroNameTok;
01894   ReadMacroName(MacroNameTok);
01895 
01896   // Error reading macro name?  If so, diagnostic already issued.
01897   if (MacroNameTok.is(tok::eod)) {
01898     // Skip code until we get to #endif.  This helps with recovery by not
01899     // emitting an error when the #endif is reached.
01900     SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
01901                                  /*Foundnonskip*/false, /*FoundElse*/false);
01902     return;
01903   }
01904 
01905   // Check to see if this is the last token on the #if[n]def line.
01906   CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
01907 
01908   IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
01909   MacroInfo *MI = getMacroInfo(MII);
01910 
01911   if (CurPPLexer->getConditionalStackDepth() == 0) {
01912     // If the start of a top-level #ifdef and if the macro is not defined,
01913     // inform MIOpt that this might be the start of a proper include guard.
01914     // Otherwise it is some other form of unknown conditional which we can't
01915     // handle.
01916     if (!ReadAnyTokensBeforeDirective && MI == 0) {
01917       assert(isIfndef && "#ifdef shouldn't reach here");
01918       CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII);
01919     } else
01920       CurPPLexer->MIOpt.EnterTopLevelConditional();
01921   }
01922 
01923   // If there is a macro, process it.
01924   if (MI)  // Mark it used.
01925     markMacroAsUsed(MI);
01926 
01927   if (Callbacks) {
01928     if (isIfndef)
01929       Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok);
01930     else
01931       Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok);
01932   }
01933 
01934   // Should we include the stuff contained by this directive?
01935   if (!MI == isIfndef) {
01936     // Yes, remember that we are inside a conditional, then lex the next token.
01937     CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
01938                                      /*wasskip*/false, /*foundnonskip*/true,
01939                                      /*foundelse*/false);
01940   } else {
01941     // No, skip the contents of this block.
01942     SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
01943                                  /*Foundnonskip*/false,
01944                                  /*FoundElse*/false);
01945   }
01946 }
01947 
01948 /// HandleIfDirective - Implements the #if directive.
01949 ///
01950 void Preprocessor::HandleIfDirective(Token &IfToken,
01951                                      bool ReadAnyTokensBeforeDirective) {
01952   ++NumIf;
01953 
01954   // Parse and evaluate the conditional expression.
01955   IdentifierInfo *IfNDefMacro = 0;
01956   const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
01957   const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
01958   const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
01959 
01960   // If this condition is equivalent to #ifndef X, and if this is the first
01961   // directive seen, handle it for the multiple-include optimization.
01962   if (CurPPLexer->getConditionalStackDepth() == 0) {
01963     if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
01964       CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
01965     else
01966       CurPPLexer->MIOpt.EnterTopLevelConditional();
01967   }
01968 
01969   if (Callbacks)
01970     Callbacks->If(IfToken.getLocation(),
01971                   SourceRange(ConditionalBegin, ConditionalEnd));
01972 
01973   // Should we include the stuff contained by this directive?
01974   if (ConditionalTrue) {
01975     // Yes, remember that we are inside a conditional, then lex the next token.
01976     CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
01977                                    /*foundnonskip*/true, /*foundelse*/false);
01978   } else {
01979     // No, skip the contents of this block.
01980     SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
01981                                  /*FoundElse*/false);
01982   }
01983 }
01984 
01985 /// HandleEndifDirective - Implements the #endif directive.
01986 ///
01987 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
01988   ++NumEndif;
01989 
01990   // Check that this is the whole directive.
01991   CheckEndOfDirective("endif");
01992 
01993   PPConditionalInfo CondInfo;
01994   if (CurPPLexer->popConditionalLevel(CondInfo)) {
01995     // No conditionals on the stack: this is an #endif without an #if.
01996     Diag(EndifToken, diag::err_pp_endif_without_if);
01997     return;
01998   }
01999 
02000   // If this the end of a top-level #endif, inform MIOpt.
02001   if (CurPPLexer->getConditionalStackDepth() == 0)
02002     CurPPLexer->MIOpt.ExitTopLevelConditional();
02003 
02004   assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
02005          "This code should only be reachable in the non-skipping case!");
02006 
02007   if (Callbacks)
02008     Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
02009 }
02010 
02011 /// HandleElseDirective - Implements the #else directive.
02012 ///
02013 void Preprocessor::HandleElseDirective(Token &Result) {
02014   ++NumElse;
02015 
02016   // #else directive in a non-skipping conditional... start skipping.
02017   CheckEndOfDirective("else");
02018 
02019   PPConditionalInfo CI;
02020   if (CurPPLexer->popConditionalLevel(CI)) {
02021     Diag(Result, diag::pp_err_else_without_if);
02022     return;
02023   }
02024 
02025   // If this is a top-level #else, inform the MIOpt.
02026   if (CurPPLexer->getConditionalStackDepth() == 0)
02027     CurPPLexer->MIOpt.EnterTopLevelConditional();
02028 
02029   // If this is a #else with a #else before it, report the error.
02030   if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
02031 
02032   if (Callbacks)
02033     Callbacks->Else(Result.getLocation(), CI.IfLoc);
02034 
02035   // Finally, skip the rest of the contents of this block.
02036   SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
02037                                /*FoundElse*/true, Result.getLocation());
02038 }
02039 
02040 /// HandleElifDirective - Implements the #elif directive.
02041 ///
02042 void Preprocessor::HandleElifDirective(Token &ElifToken) {
02043   ++NumElse;
02044 
02045   // #elif directive in a non-skipping conditional... start skipping.
02046   // We don't care what the condition is, because we will always skip it (since
02047   // the block immediately before it was included).
02048   const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
02049   DiscardUntilEndOfDirective();
02050   const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
02051 
02052   PPConditionalInfo CI;
02053   if (CurPPLexer->popConditionalLevel(CI)) {
02054     Diag(ElifToken, diag::pp_err_elif_without_if);
02055     return;
02056   }
02057 
02058   // If this is a top-level #elif, inform the MIOpt.
02059   if (CurPPLexer->getConditionalStackDepth() == 0)
02060     CurPPLexer->MIOpt.EnterTopLevelConditional();
02061 
02062   // If this is a #elif with a #else before it, report the error.
02063   if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
02064   
02065   if (Callbacks)
02066     Callbacks->Elif(ElifToken.getLocation(),
02067                     SourceRange(ConditionalBegin, ConditionalEnd), CI.IfLoc);
02068 
02069   // Finally, skip the rest of the contents of this block.
02070   SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
02071                                /*FoundElse*/CI.FoundElse,
02072                                ElifToken.getLocation());
02073 }