Preprocessor.cpp 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  1. //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Chris Lattner and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Preprocessor interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Options to support:
  15. // -H - Print the name of each header file used.
  16. // -d[MDNI] - Dump various things.
  17. // -fworking-directory - #line's with preprocessor's working dir.
  18. // -fpreprocessed
  19. // -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
  20. // -W*
  21. // -w
  22. //
  23. // Messages to emit:
  24. // "Multiple include guards may be useful for:\n"
  25. //
  26. //===----------------------------------------------------------------------===//
  27. #include "clang/Lex/Preprocessor.h"
  28. #include "clang/Lex/HeaderSearch.h"
  29. #include "clang/Lex/MacroInfo.h"
  30. #include "clang/Lex/PPCallbacks.h"
  31. #include "clang/Lex/Pragma.h"
  32. #include "clang/Lex/ScratchBuffer.h"
  33. #include "clang/Basic/Diagnostic.h"
  34. #include "clang/Basic/FileManager.h"
  35. #include "clang/Basic/SourceManager.h"
  36. #include "clang/Basic/TargetInfo.h"
  37. #include "llvm/ADT/SmallVector.h"
  38. #include <iostream>
  39. using namespace clang;
  40. //===----------------------------------------------------------------------===//
  41. Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
  42. TargetInfo &target, SourceManager &SM,
  43. HeaderSearch &Headers)
  44. : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
  45. SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
  46. CurLexer(0), CurDirLookup(0), CurMacroExpander(0), Callbacks(0) {
  47. ScratchBuf = new ScratchBuffer(SourceMgr);
  48. // Clear stats.
  49. NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
  50. NumIf = NumElse = NumEndif = 0;
  51. NumEnteredSourceFiles = 0;
  52. NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
  53. NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
  54. MaxIncludeStackDepth = 0;
  55. NumSkipped = 0;
  56. // Default to discarding comments.
  57. KeepComments = false;
  58. KeepMacroComments = false;
  59. // Macro expansion is enabled.
  60. DisableMacroExpansion = false;
  61. InMacroArgs = false;
  62. // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
  63. // This gets unpoisoned where it is allowed.
  64. (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
  65. // Initialize the pragma handlers.
  66. PragmaHandlers = new PragmaNamespace(0);
  67. RegisterBuiltinPragmas();
  68. // Initialize builtin macros like __LINE__ and friends.
  69. RegisterBuiltinMacros();
  70. }
  71. Preprocessor::~Preprocessor() {
  72. // Free any active lexers.
  73. delete CurLexer;
  74. while (!IncludeMacroStack.empty()) {
  75. delete IncludeMacroStack.back().TheLexer;
  76. delete IncludeMacroStack.back().TheMacroExpander;
  77. IncludeMacroStack.pop_back();
  78. }
  79. // Release pragma information.
  80. delete PragmaHandlers;
  81. // Delete the scratch buffer info.
  82. delete ScratchBuf;
  83. }
  84. PPCallbacks::~PPCallbacks() {
  85. }
  86. /// Diag - Forwarding function for diagnostics. This emits a diagnostic at
  87. /// the specified LexerToken's location, translating the token's start
  88. /// position in the current buffer into a SourcePosition object for rendering.
  89. void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
  90. Diags.Report(Loc, DiagID);
  91. }
  92. void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
  93. const std::string &Msg) {
  94. Diags.Report(Loc, DiagID, &Msg, 1);
  95. }
  96. void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const {
  97. std::cerr << tok::getTokenName(Tok.getKind()) << " '"
  98. << getSpelling(Tok) << "'";
  99. if (!DumpFlags) return;
  100. std::cerr << "\t";
  101. if (Tok.isAtStartOfLine())
  102. std::cerr << " [StartOfLine]";
  103. if (Tok.hasLeadingSpace())
  104. std::cerr << " [LeadingSpace]";
  105. if (Tok.isExpandDisabled())
  106. std::cerr << " [ExpandDisabled]";
  107. if (Tok.needsCleaning()) {
  108. const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
  109. std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
  110. << "']";
  111. }
  112. }
  113. void Preprocessor::DumpMacro(const MacroInfo &MI) const {
  114. std::cerr << "MACRO: ";
  115. for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
  116. DumpToken(MI.getReplacementToken(i));
  117. std::cerr << " ";
  118. }
  119. std::cerr << "\n";
  120. }
  121. void Preprocessor::PrintStats() {
  122. std::cerr << "\n*** Preprocessor Stats:\n";
  123. std::cerr << NumDirectives << " directives found:\n";
  124. std::cerr << " " << NumDefined << " #define.\n";
  125. std::cerr << " " << NumUndefined << " #undef.\n";
  126. std::cerr << " #include/#include_next/#import:\n";
  127. std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
  128. std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
  129. std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
  130. std::cerr << " " << NumElse << " #else/#elif.\n";
  131. std::cerr << " " << NumEndif << " #endif.\n";
  132. std::cerr << " " << NumPragma << " #pragma.\n";
  133. std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
  134. std::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
  135. << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
  136. << NumFastMacroExpanded << " on the fast path.\n";
  137. std::cerr << (NumFastTokenPaste+NumTokenPaste)
  138. << " token paste (##) operations performed, "
  139. << NumFastTokenPaste << " on the fast path.\n";
  140. }
  141. //===----------------------------------------------------------------------===//
  142. // Token Spelling
  143. //===----------------------------------------------------------------------===//
  144. /// getSpelling() - Return the 'spelling' of this token. The spelling of a
  145. /// token are the characters used to represent the token in the source file
  146. /// after trigraph expansion and escaped-newline folding. In particular, this
  147. /// wants to get the true, uncanonicalized, spelling of things like digraphs
  148. /// UCNs, etc.
  149. std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
  150. assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
  151. // If this token contains nothing interesting, return it directly.
  152. const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
  153. if (!Tok.needsCleaning())
  154. return std::string(TokStart, TokStart+Tok.getLength());
  155. std::string Result;
  156. Result.reserve(Tok.getLength());
  157. // Otherwise, hard case, relex the characters into the string.
  158. for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
  159. Ptr != End; ) {
  160. unsigned CharSize;
  161. Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
  162. Ptr += CharSize;
  163. }
  164. assert(Result.size() != unsigned(Tok.getLength()) &&
  165. "NeedsCleaning flag set on something that didn't need cleaning!");
  166. return Result;
  167. }
  168. /// getSpelling - This method is used to get the spelling of a token into a
  169. /// preallocated buffer, instead of as an std::string. The caller is required
  170. /// to allocate enough space for the token, which is guaranteed to be at least
  171. /// Tok.getLength() bytes long. The actual length of the token is returned.
  172. ///
  173. /// Note that this method may do two possible things: it may either fill in
  174. /// the buffer specified with characters, or it may *change the input pointer*
  175. /// to point to a constant buffer with the data already in it (avoiding a
  176. /// copy). The caller is not allowed to modify the returned buffer pointer
  177. /// if an internal buffer is returned.
  178. unsigned Preprocessor::getSpelling(const LexerToken &Tok,
  179. const char *&Buffer) const {
  180. assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
  181. // If this token is an identifier, just return the string from the identifier
  182. // table, which is very quick.
  183. if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
  184. Buffer = II->getName();
  185. return Tok.getLength();
  186. }
  187. // Otherwise, compute the start of the token in the input lexer buffer.
  188. const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
  189. // If this token contains nothing interesting, return it directly.
  190. if (!Tok.needsCleaning()) {
  191. Buffer = TokStart;
  192. return Tok.getLength();
  193. }
  194. // Otherwise, hard case, relex the characters into the string.
  195. char *OutBuf = const_cast<char*>(Buffer);
  196. for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
  197. Ptr != End; ) {
  198. unsigned CharSize;
  199. *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
  200. Ptr += CharSize;
  201. }
  202. assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
  203. "NeedsCleaning flag set on something that didn't need cleaning!");
  204. return OutBuf-Buffer;
  205. }
  206. /// CreateString - Plop the specified string into a scratch buffer and return a
  207. /// location for it. If specified, the source location provides a source
  208. /// location for the token.
  209. SourceLocation Preprocessor::
  210. CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
  211. if (SLoc.isValid())
  212. return ScratchBuf->getToken(Buf, Len, SLoc);
  213. return ScratchBuf->getToken(Buf, Len);
  214. }
  215. //===----------------------------------------------------------------------===//
  216. // Source File Location Methods.
  217. //===----------------------------------------------------------------------===//
  218. /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
  219. /// return null on failure. isAngled indicates whether the file reference is
  220. /// for system #include's or not (i.e. using <> instead of "").
  221. const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
  222. const char *FilenameEnd,
  223. bool isAngled,
  224. const DirectoryLookup *FromDir,
  225. const DirectoryLookup *&CurDir) {
  226. // If the header lookup mechanism may be relative to the current file, pass in
  227. // info about where the current file is.
  228. const FileEntry *CurFileEnt = 0;
  229. if (!FromDir) {
  230. unsigned TheFileID = getCurrentFileLexer()->getCurFileID();
  231. CurFileEnt = SourceMgr.getFileEntryForFileID(TheFileID);
  232. }
  233. // Do a standard file entry lookup.
  234. CurDir = CurDirLookup;
  235. const FileEntry *FE =
  236. HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
  237. isAngled, FromDir, CurDir, CurFileEnt);
  238. if (FE) return FE;
  239. // Otherwise, see if this is a subframework header. If so, this is relative
  240. // to one of the headers on the #include stack. Walk the list of the current
  241. // headers on the #include stack and pass them to HeaderInfo.
  242. if (CurLexer && !CurLexer->Is_PragmaLexer) {
  243. CurFileEnt = SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
  244. if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
  245. CurFileEnt)))
  246. return FE;
  247. }
  248. for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
  249. IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
  250. if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
  251. CurFileEnt =
  252. SourceMgr.getFileEntryForFileID(ISEntry.TheLexer->getCurFileID());
  253. if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
  254. CurFileEnt)))
  255. return FE;
  256. }
  257. }
  258. // Otherwise, we really couldn't find the file.
  259. return 0;
  260. }
  261. /// isInPrimaryFile - Return true if we're in the top-level file, not in a
  262. /// #include.
  263. bool Preprocessor::isInPrimaryFile() const {
  264. if (CurLexer && !CurLexer->Is_PragmaLexer)
  265. return CurLexer->isMainFile();
  266. // If there are any stacked lexers, we're in a #include.
  267. for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i)
  268. if (IncludeMacroStack[i].TheLexer &&
  269. !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
  270. return IncludeMacroStack[i].TheLexer->isMainFile();
  271. return false;
  272. }
  273. /// getCurrentLexer - Return the current file lexer being lexed from. Note
  274. /// that this ignores any potentially active macro expansions and _Pragma
  275. /// expansions going on at the time.
  276. Lexer *Preprocessor::getCurrentFileLexer() const {
  277. if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
  278. // Look for a stacked lexer.
  279. for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
  280. Lexer *L = IncludeMacroStack[i-1].TheLexer;
  281. if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
  282. return L;
  283. }
  284. return 0;
  285. }
  286. /// EnterSourceFile - Add a source file to the top of the include stack and
  287. /// start lexing tokens from it instead of the current buffer. Return true
  288. /// on failure.
  289. void Preprocessor::EnterSourceFile(unsigned FileID,
  290. const DirectoryLookup *CurDir,
  291. bool isMainFile) {
  292. assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
  293. ++NumEnteredSourceFiles;
  294. if (MaxIncludeStackDepth < IncludeMacroStack.size())
  295. MaxIncludeStackDepth = IncludeMacroStack.size();
  296. const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(FileID);
  297. Lexer *TheLexer = new Lexer(Buffer, FileID, *this);
  298. if (isMainFile) TheLexer->setIsMainFile();
  299. EnterSourceFileWithLexer(TheLexer, CurDir);
  300. }
  301. /// EnterSourceFile - Add a source file to the top of the include stack and
  302. /// start lexing tokens from it instead of the current buffer.
  303. void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
  304. const DirectoryLookup *CurDir) {
  305. // Add the current lexer to the include stack.
  306. if (CurLexer || CurMacroExpander)
  307. IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
  308. CurMacroExpander));
  309. CurLexer = TheLexer;
  310. CurDirLookup = CurDir;
  311. CurMacroExpander = 0;
  312. // Notify the client, if desired, that we are in a new source file.
  313. if (Callbacks && !CurLexer->Is_PragmaLexer) {
  314. DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
  315. // Get the file entry for the current file.
  316. if (const FileEntry *FE =
  317. SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
  318. FileType = HeaderInfo.getFileDirFlavor(FE);
  319. Callbacks->FileChanged(SourceLocation(CurLexer->getCurFileID(), 0),
  320. PPCallbacks::EnterFile, FileType);
  321. }
  322. }
  323. /// EnterMacro - Add a Macro to the top of the include stack and start lexing
  324. /// tokens from it instead of the current buffer.
  325. void Preprocessor::EnterMacro(LexerToken &Tok, MacroArgs *Args) {
  326. IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
  327. CurMacroExpander));
  328. CurLexer = 0;
  329. CurDirLookup = 0;
  330. CurMacroExpander = new MacroExpander(Tok, Args, *this);
  331. }
  332. /// EnterTokenStream - Add a "macro" context to the top of the include stack,
  333. /// which will cause the lexer to start returning the specified tokens. Note
  334. /// that these tokens will be re-macro-expanded when/if expansion is enabled.
  335. /// This method assumes that the specified stream of tokens has a permanent
  336. /// owner somewhere, so they do not need to be copied.
  337. void Preprocessor::EnterTokenStream(const LexerToken *Toks, unsigned NumToks) {
  338. // Save our current state.
  339. IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
  340. CurMacroExpander));
  341. CurLexer = 0;
  342. CurDirLookup = 0;
  343. // Create a macro expander to expand from the specified token stream.
  344. CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
  345. }
  346. /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
  347. /// lexer stack. This should only be used in situations where the current
  348. /// state of the top-of-stack lexer is known.
  349. void Preprocessor::RemoveTopOfLexerStack() {
  350. assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
  351. delete CurLexer;
  352. delete CurMacroExpander;
  353. CurLexer = IncludeMacroStack.back().TheLexer;
  354. CurDirLookup = IncludeMacroStack.back().TheDirLookup;
  355. CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
  356. IncludeMacroStack.pop_back();
  357. }
  358. //===----------------------------------------------------------------------===//
  359. // Macro Expansion Handling.
  360. //===----------------------------------------------------------------------===//
  361. /// RegisterBuiltinMacro - Register the specified identifier in the identifier
  362. /// table and mark it as a builtin macro to be expanded.
  363. IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
  364. // Get the identifier.
  365. IdentifierInfo *Id = getIdentifierInfo(Name);
  366. // Mark it as being a macro that is builtin.
  367. MacroInfo *MI = new MacroInfo(SourceLocation());
  368. MI->setIsBuiltinMacro();
  369. Id->setMacroInfo(MI);
  370. return Id;
  371. }
  372. /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
  373. /// identifier table.
  374. void Preprocessor::RegisterBuiltinMacros() {
  375. Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
  376. Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
  377. Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
  378. Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
  379. Ident_Pragma = RegisterBuiltinMacro("_Pragma");
  380. // GCC Extensions.
  381. Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
  382. Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
  383. Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
  384. }
  385. /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
  386. /// in its expansion, currently expands to that token literally.
  387. static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
  388. const IdentifierInfo *MacroIdent) {
  389. IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
  390. // If the token isn't an identifier, it's always literally expanded.
  391. if (II == 0) return true;
  392. // If the identifier is a macro, and if that macro is enabled, it may be
  393. // expanded so it's not a trivial expansion.
  394. if (II->getMacroInfo() && II->getMacroInfo()->isEnabled() &&
  395. // Fast expanding "#define X X" is ok, because X would be disabled.
  396. II != MacroIdent)
  397. return false;
  398. // If this is an object-like macro invocation, it is safe to trivially expand
  399. // it.
  400. if (MI->isObjectLike()) return true;
  401. // If this is a function-like macro invocation, it's safe to trivially expand
  402. // as long as the identifier is not a macro argument.
  403. for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
  404. I != E; ++I)
  405. if (*I == II)
  406. return false; // Identifier is a macro argument.
  407. return true;
  408. }
  409. /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
  410. /// lexed is a '('. If so, consume the token and return true, if not, this
  411. /// method should have no observable side-effect on the lexed tokens.
  412. bool Preprocessor::isNextPPTokenLParen() {
  413. // Do some quick tests for rejection cases.
  414. unsigned Val;
  415. if (CurLexer)
  416. Val = CurLexer->isNextPPTokenLParen();
  417. else
  418. Val = CurMacroExpander->isNextTokenLParen();
  419. if (Val == 2) {
  420. // If we ran off the end of the lexer or macro expander, walk the include
  421. // stack, looking for whatever will return the next token.
  422. for (unsigned i = IncludeMacroStack.size(); Val == 2 && i != 0; --i) {
  423. IncludeStackInfo &Entry = IncludeMacroStack[i-1];
  424. if (Entry.TheLexer)
  425. Val = Entry.TheLexer->isNextPPTokenLParen();
  426. else
  427. Val = Entry.TheMacroExpander->isNextTokenLParen();
  428. }
  429. }
  430. // Okay, if we know that the token is a '(', lex it and return. Otherwise we
  431. // have found something that isn't a '(' or we found the end of the
  432. // translation unit. In either case, return false.
  433. if (Val != 1)
  434. return false;
  435. LexerToken Tok;
  436. LexUnexpandedToken(Tok);
  437. assert(Tok.getKind() == tok::l_paren && "Error computing l-paren-ness?");
  438. return true;
  439. }
  440. /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
  441. /// expanded as a macro, handle it and return the next token as 'Identifier'.
  442. bool Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
  443. MacroInfo *MI) {
  444. // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
  445. if (MI->isBuiltinMacro()) {
  446. ExpandBuiltinMacro(Identifier);
  447. return false;
  448. }
  449. // If this is the first use of a target-specific macro, warn about it.
  450. if (MI->isTargetSpecific()) {
  451. MI->setIsTargetSpecific(false); // Don't warn on second use.
  452. getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
  453. diag::port_target_macro_use);
  454. }
  455. /// Args - If this is a function-like macro expansion, this contains,
  456. /// for each macro argument, the list of tokens that were provided to the
  457. /// invocation.
  458. MacroArgs *Args = 0;
  459. // If this is a function-like macro, read the arguments.
  460. if (MI->isFunctionLike()) {
  461. // C99 6.10.3p10: If the preprocessing token immediately after the the macro
  462. // name isn't a '(', this macro should not be expanded.
  463. if (!isNextPPTokenLParen())
  464. return true;
  465. // Remember that we are now parsing the arguments to a macro invocation.
  466. // Preprocessor directives used inside macro arguments are not portable, and
  467. // this enables the warning.
  468. InMacroArgs = true;
  469. Args = ReadFunctionLikeMacroArgs(Identifier, MI);
  470. // Finished parsing args.
  471. InMacroArgs = false;
  472. // If there was an error parsing the arguments, bail out.
  473. if (Args == 0) return false;
  474. ++NumFnMacroExpanded;
  475. } else {
  476. ++NumMacroExpanded;
  477. }
  478. // Notice that this macro has been used.
  479. MI->setIsUsed(true);
  480. // If we started lexing a macro, enter the macro expansion body.
  481. // If this macro expands to no tokens, don't bother to push it onto the
  482. // expansion stack, only to take it right back off.
  483. if (MI->getNumTokens() == 0) {
  484. // No need for arg info.
  485. if (Args) Args->destroy();
  486. // Ignore this macro use, just return the next token in the current
  487. // buffer.
  488. bool HadLeadingSpace = Identifier.hasLeadingSpace();
  489. bool IsAtStartOfLine = Identifier.isAtStartOfLine();
  490. Lex(Identifier);
  491. // If the identifier isn't on some OTHER line, inherit the leading
  492. // whitespace/first-on-a-line property of this token. This handles
  493. // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
  494. // empty.
  495. if (!Identifier.isAtStartOfLine()) {
  496. if (IsAtStartOfLine) Identifier.setFlag(LexerToken::StartOfLine);
  497. if (HadLeadingSpace) Identifier.setFlag(LexerToken::LeadingSpace);
  498. }
  499. ++NumFastMacroExpanded;
  500. return false;
  501. } else if (MI->getNumTokens() == 1 &&
  502. isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo())){
  503. // Otherwise, if this macro expands into a single trivially-expanded
  504. // token: expand it now. This handles common cases like
  505. // "#define VAL 42".
  506. // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
  507. // identifier to the expanded token.
  508. bool isAtStartOfLine = Identifier.isAtStartOfLine();
  509. bool hasLeadingSpace = Identifier.hasLeadingSpace();
  510. // Remember where the token is instantiated.
  511. SourceLocation InstantiateLoc = Identifier.getLocation();
  512. // Replace the result token.
  513. Identifier = MI->getReplacementToken(0);
  514. // Restore the StartOfLine/LeadingSpace markers.
  515. Identifier.setFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
  516. Identifier.setFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
  517. // Update the tokens location to include both its logical and physical
  518. // locations.
  519. SourceLocation Loc =
  520. SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
  521. Identifier.setLocation(Loc);
  522. // If this is #define X X, we must mark the result as unexpandible.
  523. if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
  524. if (NewII->getMacroInfo() == MI)
  525. Identifier.setFlag(LexerToken::DisableExpand);
  526. // Since this is not an identifier token, it can't be macro expanded, so
  527. // we're done.
  528. ++NumFastMacroExpanded;
  529. return false;
  530. }
  531. // Start expanding the macro.
  532. EnterMacro(Identifier, Args);
  533. // Now that the macro is at the top of the include stack, ask the
  534. // preprocessor to read the next token from it.
  535. Lex(Identifier);
  536. return false;
  537. }
  538. /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
  539. /// invoked to read all of the actual arguments specified for the macro
  540. /// invocation. This returns null on error.
  541. MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName,
  542. MacroInfo *MI) {
  543. // The number of fixed arguments to parse.
  544. unsigned NumFixedArgsLeft = MI->getNumArgs();
  545. bool isVariadic = MI->isVariadic();
  546. // Outer loop, while there are more arguments, keep reading them.
  547. LexerToken Tok;
  548. Tok.setKind(tok::comma);
  549. --NumFixedArgsLeft; // Start reading the first arg.
  550. // ArgTokens - Build up a list of tokens that make up each argument. Each
  551. // argument is separated by an EOF token. Use a SmallVector so we can avoid
  552. // heap allocations in the common case.
  553. llvm::SmallVector<LexerToken, 64> ArgTokens;
  554. unsigned NumActuals = 0;
  555. while (Tok.getKind() == tok::comma) {
  556. // C99 6.10.3p11: Keep track of the number of l_parens we have seen.
  557. unsigned NumParens = 0;
  558. while (1) {
  559. // Read arguments as unexpanded tokens. This avoids issues, e.g., where
  560. // an argument value in a macro could expand to ',' or '(' or ')'.
  561. LexUnexpandedToken(Tok);
  562. if (Tok.getKind() == tok::eof) {
  563. Diag(MacroName, diag::err_unterm_macro_invoc);
  564. // Do not lose the EOF. Return it to the client.
  565. MacroName = Tok;
  566. return 0;
  567. } else if (Tok.getKind() == tok::r_paren) {
  568. // If we found the ) token, the macro arg list is done.
  569. if (NumParens-- == 0)
  570. break;
  571. } else if (Tok.getKind() == tok::l_paren) {
  572. ++NumParens;
  573. } else if (Tok.getKind() == tok::comma && NumParens == 0) {
  574. // Comma ends this argument if there are more fixed arguments expected.
  575. if (NumFixedArgsLeft)
  576. break;
  577. // If this is not a variadic macro, too many args were specified.
  578. if (!isVariadic) {
  579. // Emit the diagnostic at the macro name in case there is a missing ).
  580. // Emitting it at the , could be far away from the macro name.
  581. Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
  582. return 0;
  583. }
  584. // Otherwise, continue to add the tokens to this variable argument.
  585. } else if (Tok.getKind() == tok::comment && !KeepMacroComments) {
  586. // If this is a comment token in the argument list and we're just in
  587. // -C mode (not -CC mode), discard the comment.
  588. continue;
  589. }
  590. ArgTokens.push_back(Tok);
  591. }
  592. // Empty arguments are standard in C99 and supported as an extension in
  593. // other modes.
  594. if (ArgTokens.empty() && !Features.C99)
  595. Diag(Tok, diag::ext_empty_fnmacro_arg);
  596. // Add a marker EOF token to the end of the token list for this argument.
  597. LexerToken EOFTok;
  598. EOFTok.startToken();
  599. EOFTok.setKind(tok::eof);
  600. EOFTok.setLocation(Tok.getLocation());
  601. EOFTok.setLength(0);
  602. ArgTokens.push_back(EOFTok);
  603. ++NumActuals;
  604. --NumFixedArgsLeft;
  605. };
  606. // Okay, we either found the r_paren. Check to see if we parsed too few
  607. // arguments.
  608. unsigned MinArgsExpected = MI->getNumArgs();
  609. // See MacroArgs instance var for description of this.
  610. bool isVarargsElided = false;
  611. if (NumActuals < MinArgsExpected) {
  612. // There are several cases where too few arguments is ok, handle them now.
  613. if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
  614. // Varargs where the named vararg parameter is missing: ok as extension.
  615. // #define A(x, ...)
  616. // A("blah")
  617. Diag(Tok, diag::ext_missing_varargs_arg);
  618. // Remember this occurred if this is a C99 macro invocation with at least
  619. // one actual argument.
  620. isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
  621. } else if (MI->getNumArgs() == 1) {
  622. // #define A(x)
  623. // A()
  624. // is ok because it is an empty argument.
  625. // Empty arguments are standard in C99 and supported as an extension in
  626. // other modes.
  627. if (ArgTokens.empty() && !Features.C99)
  628. Diag(Tok, diag::ext_empty_fnmacro_arg);
  629. } else {
  630. // Otherwise, emit the error.
  631. Diag(Tok, diag::err_too_few_args_in_macro_invoc);
  632. return 0;
  633. }
  634. // Add a marker EOF token to the end of the token list for this argument.
  635. SourceLocation EndLoc = Tok.getLocation();
  636. Tok.startToken();
  637. Tok.setKind(tok::eof);
  638. Tok.setLocation(EndLoc);
  639. Tok.setLength(0);
  640. ArgTokens.push_back(Tok);
  641. }
  642. return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
  643. }
  644. /// ComputeDATE_TIME - Compute the current time, enter it into the specified
  645. /// scratch buffer, then return DATELoc/TIMELoc locations with the position of
  646. /// the identifier tokens inserted.
  647. static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
  648. Preprocessor &PP) {
  649. time_t TT = time(0);
  650. struct tm *TM = localtime(&TT);
  651. static const char * const Months[] = {
  652. "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
  653. };
  654. char TmpBuffer[100];
  655. sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
  656. TM->tm_year+1900);
  657. DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
  658. sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
  659. TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
  660. }
  661. /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
  662. /// as a builtin macro, handle it and return the next token as 'Tok'.
  663. void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
  664. // Figure out which token this is.
  665. IdentifierInfo *II = Tok.getIdentifierInfo();
  666. assert(II && "Can't be a macro without id info!");
  667. // If this is an _Pragma directive, expand it, invoke the pragma handler, then
  668. // lex the token after it.
  669. if (II == Ident_Pragma)
  670. return Handle_Pragma(Tok);
  671. ++NumBuiltinMacroExpanded;
  672. char TmpBuffer[100];
  673. // Set up the return result.
  674. Tok.setIdentifierInfo(0);
  675. Tok.clearFlag(LexerToken::NeedsCleaning);
  676. if (II == Ident__LINE__) {
  677. // __LINE__ expands to a simple numeric value.
  678. sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation()));
  679. unsigned Length = strlen(TmpBuffer);
  680. Tok.setKind(tok::numeric_constant);
  681. Tok.setLength(Length);
  682. Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
  683. } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
  684. SourceLocation Loc = Tok.getLocation();
  685. if (II == Ident__BASE_FILE__) {
  686. Diag(Tok, diag::ext_pp_base_file);
  687. SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
  688. while (NextLoc.getFileID() != 0) {
  689. Loc = NextLoc;
  690. NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
  691. }
  692. }
  693. // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
  694. std::string FN = SourceMgr.getSourceName(Loc);
  695. FN = '"' + Lexer::Stringify(FN) + '"';
  696. Tok.setKind(tok::string_literal);
  697. Tok.setLength(FN.size());
  698. Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
  699. } else if (II == Ident__DATE__) {
  700. if (!DATELoc.isValid())
  701. ComputeDATE_TIME(DATELoc, TIMELoc, *this);
  702. Tok.setKind(tok::string_literal);
  703. Tok.setLength(strlen("\"Mmm dd yyyy\""));
  704. Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
  705. } else if (II == Ident__TIME__) {
  706. if (!TIMELoc.isValid())
  707. ComputeDATE_TIME(DATELoc, TIMELoc, *this);
  708. Tok.setKind(tok::string_literal);
  709. Tok.setLength(strlen("\"hh:mm:ss\""));
  710. Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
  711. } else if (II == Ident__INCLUDE_LEVEL__) {
  712. Diag(Tok, diag::ext_pp_include_level);
  713. // Compute the include depth of this token.
  714. unsigned Depth = 0;
  715. SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID());
  716. for (; Loc.getFileID() != 0; ++Depth)
  717. Loc = SourceMgr.getIncludeLoc(Loc.getFileID());
  718. // __INCLUDE_LEVEL__ expands to a simple numeric value.
  719. sprintf(TmpBuffer, "%u", Depth);
  720. unsigned Length = strlen(TmpBuffer);
  721. Tok.setKind(tok::numeric_constant);
  722. Tok.setLength(Length);
  723. Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
  724. } else if (II == Ident__TIMESTAMP__) {
  725. // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
  726. // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
  727. Diag(Tok, diag::ext_pp_timestamp);
  728. // Get the file that we are lexing out of. If we're currently lexing from
  729. // a macro, dig into the include stack.
  730. const FileEntry *CurFile = 0;
  731. Lexer *TheLexer = getCurrentFileLexer();
  732. if (TheLexer)
  733. CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
  734. // If this file is older than the file it depends on, emit a diagnostic.
  735. const char *Result;
  736. if (CurFile) {
  737. time_t TT = CurFile->getModificationTime();
  738. struct tm *TM = localtime(&TT);
  739. Result = asctime(TM);
  740. } else {
  741. Result = "??? ??? ?? ??:??:?? ????\n";
  742. }
  743. TmpBuffer[0] = '"';
  744. strcpy(TmpBuffer+1, Result);
  745. unsigned Len = strlen(TmpBuffer);
  746. TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
  747. Tok.setKind(tok::string_literal);
  748. Tok.setLength(Len);
  749. Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
  750. } else {
  751. assert(0 && "Unknown identifier!");
  752. }
  753. }
  754. //===----------------------------------------------------------------------===//
  755. // Lexer Event Handling.
  756. //===----------------------------------------------------------------------===//
  757. /// LookUpIdentifierInfo - Given a tok::identifier token, look up the
  758. /// identifier information for the token and install it into the token.
  759. IdentifierInfo *Preprocessor::LookUpIdentifierInfo(LexerToken &Identifier,
  760. const char *BufPtr) {
  761. assert(Identifier.getKind() == tok::identifier && "Not an identifier!");
  762. assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
  763. // Look up this token, see if it is a macro, or if it is a language keyword.
  764. IdentifierInfo *II;
  765. if (BufPtr && !Identifier.needsCleaning()) {
  766. // No cleaning needed, just use the characters from the lexed buffer.
  767. II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
  768. } else {
  769. // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
  770. const char *TmpBuf = (char*)alloca(Identifier.getLength());
  771. unsigned Size = getSpelling(Identifier, TmpBuf);
  772. II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
  773. }
  774. Identifier.setIdentifierInfo(II);
  775. return II;
  776. }
  777. /// HandleIdentifier - This callback is invoked when the lexer reads an
  778. /// identifier. This callback looks up the identifier in the map and/or
  779. /// potentially macro expands it or turns it into a named token (like 'for').
  780. void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
  781. assert(Identifier.getIdentifierInfo() &&
  782. "Can't handle identifiers without identifier info!");
  783. IdentifierInfo &II = *Identifier.getIdentifierInfo();
  784. // If this identifier was poisoned, and if it was not produced from a macro
  785. // expansion, emit an error.
  786. if (II.isPoisoned() && CurLexer) {
  787. if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
  788. Diag(Identifier, diag::err_pp_used_poisoned_id);
  789. else
  790. Diag(Identifier, diag::ext_pp_bad_vaargs_use);
  791. }
  792. // If this is a macro to be expanded, do it.
  793. if (MacroInfo *MI = II.getMacroInfo()) {
  794. if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
  795. if (MI->isEnabled()) {
  796. if (!HandleMacroExpandedIdentifier(Identifier, MI))
  797. return;
  798. } else {
  799. // C99 6.10.3.4p2 says that a disabled macro may never again be
  800. // expanded, even if it's in a context where it could be expanded in the
  801. // future.
  802. Identifier.setFlag(LexerToken::DisableExpand);
  803. }
  804. }
  805. } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
  806. // If this identifier is a macro on some other target, emit a diagnostic.
  807. // This diagnosic is only emitted when macro expansion is enabled, because
  808. // the macro would not have been expanded for the other target either.
  809. II.setIsOtherTargetMacro(false); // Don't warn on second use.
  810. getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
  811. diag::port_target_macro_use);
  812. }
  813. // C++ 2.11p2: If this is an alternative representation of a C++ operator,
  814. // then we act as if it is the actual operator and not the textual
  815. // representation of it.
  816. if (II.isCPlusPlusOperatorKeyword())
  817. Identifier.setIdentifierInfo(0);
  818. // Change the kind of this identifier to the appropriate token kind, e.g.
  819. // turning "for" into a keyword.
  820. Identifier.setKind(II.getTokenID());
  821. // If this is an extension token, diagnose its use.
  822. // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
  823. // For now, I'm just commenting it out (while I work on attributes).
  824. if (II.isExtensionToken() && Features.C99)
  825. Diag(Identifier, diag::ext_token_used);
  826. }
  827. /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
  828. /// the current file. This either returns the EOF token or pops a level off
  829. /// the include stack and keeps going.
  830. bool Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
  831. assert(!CurMacroExpander &&
  832. "Ending a file when currently in a macro!");
  833. // See if this file had a controlling macro.
  834. if (CurLexer) { // Not ending a macro, ignore it.
  835. if (const IdentifierInfo *ControllingMacro =
  836. CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
  837. // Okay, this has a controlling macro, remember in PerFileInfo.
  838. if (const FileEntry *FE =
  839. SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
  840. HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
  841. }
  842. }
  843. // If this is a #include'd file, pop it off the include stack and continue
  844. // lexing the #includer file.
  845. if (!IncludeMacroStack.empty()) {
  846. // We're done with the #included file.
  847. RemoveTopOfLexerStack();
  848. // Notify the client, if desired, that we are in a new source file.
  849. if (Callbacks && !isEndOfMacro && CurLexer) {
  850. DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
  851. // Get the file entry for the current file.
  852. if (const FileEntry *FE =
  853. SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
  854. FileType = HeaderInfo.getFileDirFlavor(FE);
  855. Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
  856. PPCallbacks::ExitFile, FileType);
  857. }
  858. // Client should lex another token.
  859. return false;
  860. }
  861. Result.startToken();
  862. CurLexer->BufferPtr = CurLexer->BufferEnd;
  863. CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
  864. Result.setKind(tok::eof);
  865. // We're done with the #included file.
  866. delete CurLexer;
  867. CurLexer = 0;
  868. // This is the end of the top-level file. If the diag::pp_macro_not_used
  869. // diagnostic is enabled, walk all of the identifiers, looking for macros that
  870. // have not been used.
  871. if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
  872. for (IdentifierTable::iterator I = Identifiers.begin(),
  873. E = Identifiers.end(); I != E; ++I) {
  874. const IdentifierInfo &II = I->getValue();
  875. if (II.getMacroInfo() && !II.getMacroInfo()->isUsed())
  876. Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
  877. }
  878. }
  879. return true;
  880. }
  881. /// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
  882. /// the current macro expansion or token stream expansion.
  883. bool Preprocessor::HandleEndOfMacro(LexerToken &Result) {
  884. assert(CurMacroExpander && !CurLexer &&
  885. "Ending a macro when currently in a #include file!");
  886. delete CurMacroExpander;
  887. // Handle this like a #include file being popped off the stack.
  888. CurMacroExpander = 0;
  889. return HandleEndOfFile(Result, true);
  890. }
  891. //===----------------------------------------------------------------------===//
  892. // Utility Methods for Preprocessor Directive Handling.
  893. //===----------------------------------------------------------------------===//
  894. /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
  895. /// current line until the tok::eom token is found.
  896. void Preprocessor::DiscardUntilEndOfDirective() {
  897. LexerToken Tmp;
  898. do {
  899. LexUnexpandedToken(Tmp);
  900. } while (Tmp.getKind() != tok::eom);
  901. }
  902. /// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
  903. static bool isCXXNamedOperator(const std::string &Spelling) {
  904. return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
  905. Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
  906. Spelling == "or" || Spelling == "xor";
  907. }
  908. /// ReadMacroName - Lex and validate a macro name, which occurs after a
  909. /// #define or #undef. This sets the token kind to eom and discards the rest
  910. /// of the macro line if the macro name is invalid. isDefineUndef is 1 if
  911. /// this is due to a a #define, 2 if #undef directive, 0 if it is something
  912. /// else (e.g. #ifdef).
  913. void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) {
  914. // Read the token, don't allow macro expansion on it.
  915. LexUnexpandedToken(MacroNameTok);
  916. // Missing macro name?
  917. if (MacroNameTok.getKind() == tok::eom)
  918. return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
  919. IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
  920. if (II == 0) {
  921. std::string Spelling = getSpelling(MacroNameTok);
  922. if (isCXXNamedOperator(Spelling))
  923. // C++ 2.5p2: Alternative tokens behave the same as its primary token
  924. // except for their spellings.
  925. Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
  926. else
  927. Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
  928. // Fall through on error.
  929. } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
  930. // Error if defining "defined": C99 6.10.8.4.
  931. Diag(MacroNameTok, diag::err_defined_macro_name);
  932. } else if (isDefineUndef && II->getMacroInfo() &&
  933. II->getMacroInfo()->isBuiltinMacro()) {
  934. // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
  935. if (isDefineUndef == 1)
  936. Diag(MacroNameTok, diag::pp_redef_builtin_macro);
  937. else
  938. Diag(MacroNameTok, diag::pp_undef_builtin_macro);
  939. } else {
  940. // Okay, we got a good identifier node. Return it.
  941. return;
  942. }
  943. // Invalid macro name, read and discard the rest of the line. Then set the
  944. // token kind to tok::eom.
  945. MacroNameTok.setKind(tok::eom);
  946. return DiscardUntilEndOfDirective();
  947. }
  948. /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
  949. /// not, emit a diagnostic and consume up until the eom.
  950. void Preprocessor::CheckEndOfDirective(const char *DirType) {
  951. LexerToken Tmp;
  952. Lex(Tmp);
  953. // There should be no tokens after the directive, but we allow them as an
  954. // extension.
  955. while (Tmp.getKind() == tok::comment) // Skip comments in -C mode.
  956. Lex(Tmp);
  957. if (Tmp.getKind() != tok::eom) {
  958. Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
  959. DiscardUntilEndOfDirective();
  960. }
  961. }
  962. /// SkipExcludedConditionalBlock - We just read a #if or related directive and
  963. /// decided that the subsequent tokens are in the #if'd out portion of the
  964. /// file. Lex the rest of the file, until we see an #endif. If
  965. /// FoundNonSkipPortion is true, then we have already emitted code for part of
  966. /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
  967. /// is true, then #else directives are ok, if not, then we have already seen one
  968. /// so a #else directive is a duplicate. When this returns, the caller can lex
  969. /// the first valid token.
  970. void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
  971. bool FoundNonSkipPortion,
  972. bool FoundElse) {
  973. ++NumSkipped;
  974. assert(CurMacroExpander == 0 && CurLexer &&
  975. "Lexing a macro, not a file?");
  976. CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
  977. FoundNonSkipPortion, FoundElse);
  978. // Enter raw mode to disable identifier lookup (and thus macro expansion),
  979. // disabling warnings, etc.
  980. CurLexer->LexingRawMode = true;
  981. LexerToken Tok;
  982. while (1) {
  983. CurLexer->Lex(Tok);
  984. // If this is the end of the buffer, we have an error.
  985. if (Tok.getKind() == tok::eof) {
  986. // Emit errors for each unterminated conditional on the stack, including
  987. // the current one.
  988. while (!CurLexer->ConditionalStack.empty()) {
  989. Diag(CurLexer->ConditionalStack.back().IfLoc,
  990. diag::err_pp_unterminated_conditional);
  991. CurLexer->ConditionalStack.pop_back();
  992. }
  993. // Just return and let the caller lex after this #include.
  994. break;
  995. }
  996. // If this token is not a preprocessor directive, just skip it.
  997. if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
  998. continue;
  999. // We just parsed a # character at the start of a line, so we're in
  1000. // directive mode. Tell the lexer this so any newlines we see will be
  1001. // converted into an EOM token (this terminates the macro).
  1002. CurLexer->ParsingPreprocessorDirective = true;
  1003. CurLexer->KeepCommentMode = false;
  1004. // Read the next token, the directive flavor.
  1005. LexUnexpandedToken(Tok);
  1006. // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
  1007. // something bogus), skip it.
  1008. if (Tok.getKind() != tok::identifier) {
  1009. CurLexer->ParsingPreprocessorDirective = false;
  1010. // Restore comment saving mode.
  1011. CurLexer->KeepCommentMode = KeepComments;
  1012. continue;
  1013. }
  1014. // If the first letter isn't i or e, it isn't intesting to us. We know that
  1015. // this is safe in the face of spelling differences, because there is no way
  1016. // to spell an i/e in a strange way that is another letter. Skipping this
  1017. // allows us to avoid looking up the identifier info for #define/#undef and
  1018. // other common directives.
  1019. const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
  1020. char FirstChar = RawCharData[0];
  1021. if (FirstChar >= 'a' && FirstChar <= 'z' &&
  1022. FirstChar != 'i' && FirstChar != 'e') {
  1023. CurLexer->ParsingPreprocessorDirective = false;
  1024. // Restore comment saving mode.
  1025. CurLexer->KeepCommentMode = KeepComments;
  1026. continue;
  1027. }
  1028. // Get the identifier name without trigraphs or embedded newlines. Note
  1029. // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
  1030. // when skipping.
  1031. // TODO: could do this with zero copies in the no-clean case by using
  1032. // strncmp below.
  1033. char Directive[20];
  1034. unsigned IdLen;
  1035. if (!Tok.needsCleaning() && Tok.getLength() < 20) {
  1036. IdLen = Tok.getLength();
  1037. memcpy(Directive, RawCharData, IdLen);
  1038. Directive[IdLen] = 0;
  1039. } else {
  1040. std::string DirectiveStr = getSpelling(Tok);
  1041. IdLen = DirectiveStr.size();
  1042. if (IdLen >= 20) {
  1043. CurLexer->ParsingPreprocessorDirective = false;
  1044. // Restore comment saving mode.
  1045. CurLexer->KeepCommentMode = KeepComments;
  1046. continue;
  1047. }
  1048. memcpy(Directive, &DirectiveStr[0], IdLen);
  1049. Directive[IdLen] = 0;
  1050. }
  1051. if (FirstChar == 'i' && Directive[1] == 'f') {
  1052. if ((IdLen == 2) || // "if"
  1053. (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
  1054. (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
  1055. // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
  1056. // bother parsing the condition.
  1057. DiscardUntilEndOfDirective();
  1058. CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
  1059. /*foundnonskip*/false,
  1060. /*fnddelse*/false);
  1061. }
  1062. } else if (FirstChar == 'e') {
  1063. if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
  1064. CheckEndOfDirective("#endif");
  1065. PPConditionalInfo CondInfo;
  1066. CondInfo.WasSkipping = true; // Silence bogus warning.
  1067. bool InCond = CurLexer->popConditionalLevel(CondInfo);
  1068. InCond = InCond; // Silence warning in no-asserts mode.
  1069. assert(!InCond && "Can't be skipping if not in a conditional!");
  1070. // If we popped the outermost skipping block, we're done skipping!
  1071. if (!CondInfo.WasSkipping)
  1072. break;
  1073. } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
  1074. // #else directive in a skipping conditional. If not in some other
  1075. // skipping conditional, and if #else hasn't already been seen, enter it
  1076. // as a non-skipping conditional.
  1077. CheckEndOfDirective("#else");
  1078. PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
  1079. // If this is a #else with a #else before it, report the error.
  1080. if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
  1081. // Note that we've seen a #else in this conditional.
  1082. CondInfo.FoundElse = true;
  1083. // If the conditional is at the top level, and the #if block wasn't
  1084. // entered, enter the #else block now.
  1085. if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
  1086. CondInfo.FoundNonSkip = true;
  1087. break;
  1088. }
  1089. } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
  1090. PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
  1091. bool ShouldEnter;
  1092. // If this is in a skipping block or if we're already handled this #if
  1093. // block, don't bother parsing the condition.
  1094. if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
  1095. DiscardUntilEndOfDirective();
  1096. ShouldEnter = false;
  1097. } else {
  1098. // Restore the value of LexingRawMode so that identifiers are
  1099. // looked up, etc, inside the #elif expression.
  1100. assert(CurLexer->LexingRawMode && "We have to be skipping here!");
  1101. CurLexer->LexingRawMode = false;
  1102. IdentifierInfo *IfNDefMacro = 0;
  1103. ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
  1104. CurLexer->LexingRawMode = true;
  1105. }
  1106. // If this is a #elif with a #else before it, report the error.
  1107. if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
  1108. // If this condition is true, enter it!
  1109. if (ShouldEnter) {
  1110. CondInfo.FoundNonSkip = true;
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. CurLexer->ParsingPreprocessorDirective = false;
  1116. // Restore comment saving mode.
  1117. CurLexer->KeepCommentMode = KeepComments;
  1118. }
  1119. // Finally, if we are out of the conditional (saw an #endif or ran off the end
  1120. // of the file, just stop skipping and return to lexing whatever came after
  1121. // the #if block.
  1122. CurLexer->LexingRawMode = false;
  1123. }
  1124. //===----------------------------------------------------------------------===//
  1125. // Preprocessor Directive Handling.
  1126. //===----------------------------------------------------------------------===//
  1127. /// HandleDirective - This callback is invoked when the lexer sees a # token
  1128. /// at the start of a line. This consumes the directive, modifies the
  1129. /// lexer/preprocessor state, and advances the lexer(s) so that the next token
  1130. /// read is the correct one.
  1131. void Preprocessor::HandleDirective(LexerToken &Result) {
  1132. // FIXME: Traditional: # with whitespace before it not recognized by K&R?
  1133. // We just parsed a # character at the start of a line, so we're in directive
  1134. // mode. Tell the lexer this so any newlines we see will be converted into an
  1135. // EOM token (which terminates the directive).
  1136. CurLexer->ParsingPreprocessorDirective = true;
  1137. ++NumDirectives;
  1138. // We are about to read a token. For the multiple-include optimization FA to
  1139. // work, we have to remember if we had read any tokens *before* this
  1140. // pp-directive.
  1141. bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
  1142. // Read the next token, the directive flavor. This isn't expanded due to
  1143. // C99 6.10.3p8.
  1144. LexUnexpandedToken(Result);
  1145. // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
  1146. // #define A(x) #x
  1147. // A(abc
  1148. // #warning blah
  1149. // def)
  1150. // If so, the user is relying on non-portable behavior, emit a diagnostic.
  1151. if (InMacroArgs)
  1152. Diag(Result, diag::ext_embedded_directive);
  1153. TryAgain:
  1154. switch (Result.getKind()) {
  1155. case tok::eom:
  1156. return; // null directive.
  1157. case tok::comment:
  1158. // Handle stuff like "# /*foo*/ define X" in -E -C mode.
  1159. LexUnexpandedToken(Result);
  1160. goto TryAgain;
  1161. case tok::numeric_constant:
  1162. // FIXME: implement # 7 line numbers!
  1163. DiscardUntilEndOfDirective();
  1164. return;
  1165. default:
  1166. IdentifierInfo *II = Result.getIdentifierInfo();
  1167. if (II == 0) break; // Not an identifier.
  1168. // Ask what the preprocessor keyword ID is.
  1169. switch (II->getPPKeywordID()) {
  1170. default: break;
  1171. // C99 6.10.1 - Conditional Inclusion.
  1172. case tok::pp_if:
  1173. return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
  1174. case tok::pp_ifdef:
  1175. return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
  1176. case tok::pp_ifndef:
  1177. return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
  1178. case tok::pp_elif:
  1179. return HandleElifDirective(Result);
  1180. case tok::pp_else:
  1181. return HandleElseDirective(Result);
  1182. case tok::pp_endif:
  1183. return HandleEndifDirective(Result);
  1184. // C99 6.10.2 - Source File Inclusion.
  1185. case tok::pp_include:
  1186. return HandleIncludeDirective(Result); // Handle #include.
  1187. // C99 6.10.3 - Macro Replacement.
  1188. case tok::pp_define:
  1189. return HandleDefineDirective(Result, false);
  1190. case tok::pp_undef:
  1191. return HandleUndefDirective(Result);
  1192. // C99 6.10.4 - Line Control.
  1193. case tok::pp_line:
  1194. // FIXME: implement #line
  1195. DiscardUntilEndOfDirective();
  1196. return;
  1197. // C99 6.10.5 - Error Directive.
  1198. case tok::pp_error:
  1199. return HandleUserDiagnosticDirective(Result, false);
  1200. // C99 6.10.6 - Pragma Directive.
  1201. case tok::pp_pragma:
  1202. return HandlePragmaDirective();
  1203. // GNU Extensions.
  1204. case tok::pp_import:
  1205. return HandleImportDirective(Result);
  1206. case tok::pp_include_next:
  1207. return HandleIncludeNextDirective(Result);
  1208. case tok::pp_warning:
  1209. Diag(Result, diag::ext_pp_warning_directive);
  1210. return HandleUserDiagnosticDirective(Result, true);
  1211. case tok::pp_ident:
  1212. return HandleIdentSCCSDirective(Result);
  1213. case tok::pp_sccs:
  1214. return HandleIdentSCCSDirective(Result);
  1215. case tok::pp_assert:
  1216. //isExtension = true; // FIXME: implement #assert
  1217. break;
  1218. case tok::pp_unassert:
  1219. //isExtension = true; // FIXME: implement #unassert
  1220. break;
  1221. // clang extensions.
  1222. case tok::pp_define_target:
  1223. return HandleDefineDirective(Result, true);
  1224. case tok::pp_define_other_target:
  1225. return HandleDefineOtherTargetDirective(Result);
  1226. }
  1227. break;
  1228. }
  1229. // If we reached here, the preprocessing token is not valid!
  1230. Diag(Result, diag::err_pp_invalid_directive);
  1231. // Read the rest of the PP line.
  1232. DiscardUntilEndOfDirective();
  1233. // Okay, we're done parsing the directive.
  1234. }
  1235. void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok,
  1236. bool isWarning) {
  1237. // Read the rest of the line raw. We do this because we don't want macros
  1238. // to be expanded and we don't require that the tokens be valid preprocessing
  1239. // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
  1240. // collapse multiple consequtive white space between tokens, but this isn't
  1241. // specified by the standard.
  1242. std::string Message = CurLexer->ReadToEndOfLine();
  1243. unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
  1244. return Diag(Tok, DiagID, Message);
  1245. }
  1246. /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
  1247. ///
  1248. void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) {
  1249. // Yes, this directive is an extension.
  1250. Diag(Tok, diag::ext_pp_ident_directive);
  1251. // Read the string argument.
  1252. LexerToken StrTok;
  1253. Lex(StrTok);
  1254. // If the token kind isn't a string, it's a malformed directive.
  1255. if (StrTok.getKind() != tok::string_literal &&
  1256. StrTok.getKind() != tok::wide_string_literal)
  1257. return Diag(StrTok, diag::err_pp_malformed_ident);
  1258. // Verify that there is nothing after the string, other than EOM.
  1259. CheckEndOfDirective("#ident");
  1260. if (Callbacks)
  1261. Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
  1262. }
  1263. //===----------------------------------------------------------------------===//
  1264. // Preprocessor Include Directive Handling.
  1265. //===----------------------------------------------------------------------===//
  1266. /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
  1267. /// checked and spelled filename, e.g. as an operand of #include. This returns
  1268. /// true if the input filename was in <>'s or false if it were in ""'s. The
  1269. /// caller is expected to provide a buffer that is large enough to hold the
  1270. /// spelling of the filename, but is also expected to handle the case when
  1271. /// this method decides to use a different buffer.
  1272. bool Preprocessor::GetIncludeFilenameSpelling(const LexerToken &FilenameTok,
  1273. const char *&BufStart,
  1274. const char *&BufEnd) {
  1275. // Get the text form of the filename.
  1276. unsigned Len = getSpelling(FilenameTok, BufStart);
  1277. BufEnd = BufStart+Len;
  1278. assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
  1279. // Make sure the filename is <x> or "x".
  1280. bool isAngled;
  1281. if (BufStart[0] == '<') {
  1282. if (BufEnd[-1] != '>') {
  1283. Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
  1284. BufStart = 0;
  1285. return true;
  1286. }
  1287. isAngled = true;
  1288. } else if (BufStart[0] == '"') {
  1289. if (BufEnd[-1] != '"') {
  1290. Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
  1291. BufStart = 0;
  1292. return true;
  1293. }
  1294. isAngled = false;
  1295. } else {
  1296. Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
  1297. BufStart = 0;
  1298. return true;
  1299. }
  1300. // Diagnose #include "" as invalid.
  1301. if (BufEnd-BufStart <= 2) {
  1302. Diag(FilenameTok.getLocation(), diag::err_pp_empty_filename);
  1303. BufStart = 0;
  1304. return "";
  1305. }
  1306. // Skip the brackets.
  1307. ++BufStart;
  1308. --BufEnd;
  1309. return isAngled;
  1310. }
  1311. /// HandleIncludeDirective - The "#include" tokens have just been read, read the
  1312. /// file to be included from the lexer, then include it! This is a common
  1313. /// routine with functionality shared between #include, #include_next and
  1314. /// #import.
  1315. void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
  1316. const DirectoryLookup *LookupFrom,
  1317. bool isImport) {
  1318. LexerToken FilenameTok;
  1319. CurLexer->LexIncludeFilename(FilenameTok);
  1320. // If the token kind is EOM, the error has already been diagnosed.
  1321. if (FilenameTok.getKind() == tok::eom)
  1322. return;
  1323. // Reserve a buffer to get the spelling.
  1324. llvm::SmallVector<char, 128> FilenameBuffer;
  1325. FilenameBuffer.resize(FilenameTok.getLength());
  1326. const char *FilenameStart = &FilenameBuffer[0], *FilenameEnd;
  1327. bool isAngled = GetIncludeFilenameSpelling(FilenameTok,
  1328. FilenameStart, FilenameEnd);
  1329. // If GetIncludeFilenameSpelling set the start ptr to null, there was an
  1330. // error.
  1331. if (FilenameStart == 0)
  1332. return;
  1333. // Verify that there is nothing after the filename, other than EOM. Use the
  1334. // preprocessor to lex this in case lexing the filename entered a macro.
  1335. CheckEndOfDirective("#include");
  1336. // Check that we don't have infinite #include recursion.
  1337. if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
  1338. return Diag(FilenameTok, diag::err_pp_include_too_deep);
  1339. // Search include directories.
  1340. const DirectoryLookup *CurDir;
  1341. const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
  1342. isAngled, LookupFrom, CurDir);
  1343. if (File == 0)
  1344. return Diag(FilenameTok, diag::err_pp_file_not_found,
  1345. std::string(FilenameStart, FilenameEnd));
  1346. // Ask HeaderInfo if we should enter this #include file.
  1347. if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
  1348. // If it returns true, #including this file will have no effect.
  1349. return;
  1350. }
  1351. // Look up the file, create a File ID for it.
  1352. unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
  1353. if (FileID == 0)
  1354. return Diag(FilenameTok, diag::err_pp_file_not_found,
  1355. std::string(FilenameStart, FilenameEnd));
  1356. // Finally, if all is good, enter the new file!
  1357. EnterSourceFile(FileID, CurDir);
  1358. }
  1359. /// HandleIncludeNextDirective - Implements #include_next.
  1360. ///
  1361. void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
  1362. Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
  1363. // #include_next is like #include, except that we start searching after
  1364. // the current found directory. If we can't do this, issue a
  1365. // diagnostic.
  1366. const DirectoryLookup *Lookup = CurDirLookup;
  1367. if (isInPrimaryFile()) {
  1368. Lookup = 0;
  1369. Diag(IncludeNextTok, diag::pp_include_next_in_primary);
  1370. } else if (Lookup == 0) {
  1371. Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
  1372. } else {
  1373. // Start looking up in the next directory.
  1374. ++Lookup;
  1375. }
  1376. return HandleIncludeDirective(IncludeNextTok, Lookup);
  1377. }
  1378. /// HandleImportDirective - Implements #import.
  1379. ///
  1380. void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
  1381. Diag(ImportTok, diag::ext_pp_import_directive);
  1382. return HandleIncludeDirective(ImportTok, 0, true);
  1383. }
  1384. //===----------------------------------------------------------------------===//
  1385. // Preprocessor Macro Directive Handling.
  1386. //===----------------------------------------------------------------------===//
  1387. /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
  1388. /// definition has just been read. Lex the rest of the arguments and the
  1389. /// closing ), updating MI with what we learn. Return true if an error occurs
  1390. /// parsing the arg list.
  1391. bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
  1392. LexerToken Tok;
  1393. while (1) {
  1394. LexUnexpandedToken(Tok);
  1395. switch (Tok.getKind()) {
  1396. case tok::r_paren:
  1397. // Found the end of the argument list.
  1398. if (MI->arg_begin() == MI->arg_end()) return false; // #define FOO()
  1399. // Otherwise we have #define FOO(A,)
  1400. Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
  1401. return true;
  1402. case tok::ellipsis: // #define X(... -> C99 varargs
  1403. // Warn if use of C99 feature in non-C99 mode.
  1404. if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
  1405. // Lex the token after the identifier.
  1406. LexUnexpandedToken(Tok);
  1407. if (Tok.getKind() != tok::r_paren) {
  1408. Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
  1409. return true;
  1410. }
  1411. // Add the __VA_ARGS__ identifier as an argument.
  1412. MI->addArgument(Ident__VA_ARGS__);
  1413. MI->setIsC99Varargs();
  1414. return false;
  1415. case tok::eom: // #define X(
  1416. Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
  1417. return true;
  1418. default:
  1419. // Handle keywords and identifiers here to accept things like
  1420. // #define Foo(for) for.
  1421. IdentifierInfo *II = Tok.getIdentifierInfo();
  1422. if (II == 0) {
  1423. // #define X(1
  1424. Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
  1425. return true;
  1426. }
  1427. // If this is already used as an argument, it is used multiple times (e.g.
  1428. // #define X(A,A.
  1429. if (MI->getArgumentNum(II) != -1) { // C99 6.10.3p6
  1430. Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
  1431. return true;
  1432. }
  1433. // Add the argument to the macro info.
  1434. MI->addArgument(II);
  1435. // Lex the token after the identifier.
  1436. LexUnexpandedToken(Tok);
  1437. switch (Tok.getKind()) {
  1438. default: // #define X(A B
  1439. Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
  1440. return true;
  1441. case tok::r_paren: // #define X(A)
  1442. return false;
  1443. case tok::comma: // #define X(A,
  1444. break;
  1445. case tok::ellipsis: // #define X(A... -> GCC extension
  1446. // Diagnose extension.
  1447. Diag(Tok, diag::ext_named_variadic_macro);
  1448. // Lex the token after the identifier.
  1449. LexUnexpandedToken(Tok);
  1450. if (Tok.getKind() != tok::r_paren) {
  1451. Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
  1452. return true;
  1453. }
  1454. MI->setIsGNUVarargs();
  1455. return false;
  1456. }
  1457. }
  1458. }
  1459. }
  1460. /// HandleDefineDirective - Implements #define. This consumes the entire macro
  1461. /// line then lets the caller lex the next real token. If 'isTargetSpecific' is
  1462. /// true, then this is a "#define_target", otherwise this is a "#define".
  1463. ///
  1464. void Preprocessor::HandleDefineDirective(LexerToken &DefineTok,
  1465. bool isTargetSpecific) {
  1466. ++NumDefined;
  1467. LexerToken MacroNameTok;
  1468. ReadMacroName(MacroNameTok, 1);
  1469. // Error reading macro name? If so, diagnostic already issued.
  1470. if (MacroNameTok.getKind() == tok::eom)
  1471. return;
  1472. // If we are supposed to keep comments in #defines, reenable comment saving
  1473. // mode.
  1474. CurLexer->KeepCommentMode = KeepMacroComments;
  1475. // Create the new macro.
  1476. MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
  1477. if (isTargetSpecific) MI->setIsTargetSpecific();
  1478. // If the identifier is an 'other target' macro, clear this bit.
  1479. MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
  1480. LexerToken Tok;
  1481. LexUnexpandedToken(Tok);
  1482. // If this is a function-like macro definition, parse the argument list,
  1483. // marking each of the identifiers as being used as macro arguments. Also,
  1484. // check other constraints on the first token of the macro body.
  1485. if (Tok.getKind() == tok::eom) {
  1486. // If there is no body to this macro, we have no special handling here.
  1487. } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
  1488. // This is a function-like macro definition. Read the argument list.
  1489. MI->setIsFunctionLike();
  1490. if (ReadMacroDefinitionArgList(MI)) {
  1491. // Forget about MI.
  1492. delete MI;
  1493. // Throw away the rest of the line.
  1494. if (CurLexer->ParsingPreprocessorDirective)
  1495. DiscardUntilEndOfDirective();
  1496. return;
  1497. }
  1498. // Read the first token after the arg list for down below.
  1499. LexUnexpandedToken(Tok);
  1500. } else if (!Tok.hasLeadingSpace()) {
  1501. // C99 requires whitespace between the macro definition and the body. Emit
  1502. // a diagnostic for something like "#define X+".
  1503. if (Features.C99) {
  1504. Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
  1505. } else {
  1506. // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
  1507. // one in some cases!
  1508. }
  1509. } else {
  1510. // This is a normal token with leading space. Clear the leading space
  1511. // marker on the first token to get proper expansion.
  1512. Tok.clearFlag(LexerToken::LeadingSpace);
  1513. }
  1514. // If this is a definition of a variadic C99 function-like macro, not using
  1515. // the GNU named varargs extension, enabled __VA_ARGS__.
  1516. // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
  1517. // This gets unpoisoned where it is allowed.
  1518. assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
  1519. if (MI->isC99Varargs())
  1520. Ident__VA_ARGS__->setIsPoisoned(false);
  1521. // Read the rest of the macro body.
  1522. while (Tok.getKind() != tok::eom) {
  1523. MI->AddTokenToBody(Tok);
  1524. // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
  1525. // parameters in function-like macro expansions.
  1526. if (Tok.getKind() != tok::hash || MI->isObjectLike()) {
  1527. // Get the next token of the macro.
  1528. LexUnexpandedToken(Tok);
  1529. continue;
  1530. }
  1531. // Get the next token of the macro.
  1532. LexUnexpandedToken(Tok);
  1533. // Not a macro arg identifier?
  1534. if (!Tok.getIdentifierInfo() ||
  1535. MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
  1536. Diag(Tok, diag::err_pp_stringize_not_parameter);
  1537. delete MI;
  1538. // Disable __VA_ARGS__ again.
  1539. Ident__VA_ARGS__->setIsPoisoned(true);
  1540. return;
  1541. }
  1542. // Things look ok, add the param name token to the macro.
  1543. MI->AddTokenToBody(Tok);
  1544. // Get the next token of the macro.
  1545. LexUnexpandedToken(Tok);
  1546. }
  1547. // Disable __VA_ARGS__ again.
  1548. Ident__VA_ARGS__->setIsPoisoned(true);
  1549. // Check that there is no paste (##) operator at the begining or end of the
  1550. // replacement list.
  1551. unsigned NumTokens = MI->getNumTokens();
  1552. if (NumTokens != 0) {
  1553. if (MI->getReplacementToken(0).getKind() == tok::hashhash) {
  1554. Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
  1555. delete MI;
  1556. return;
  1557. }
  1558. if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) {
  1559. Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
  1560. delete MI;
  1561. return;
  1562. }
  1563. }
  1564. // If this is the primary source file, remember that this macro hasn't been
  1565. // used yet.
  1566. if (isInPrimaryFile())
  1567. MI->setIsUsed(false);
  1568. // Finally, if this identifier already had a macro defined for it, verify that
  1569. // the macro bodies are identical and free the old definition.
  1570. if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
  1571. if (!OtherMI->isUsed())
  1572. Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
  1573. // Macros must be identical. This means all tokes and whitespace separation
  1574. // must be the same. C99 6.10.3.2.
  1575. if (!MI->isIdenticalTo(*OtherMI, *this)) {
  1576. Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
  1577. MacroNameTok.getIdentifierInfo()->getName());
  1578. Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
  1579. }
  1580. delete OtherMI;
  1581. }
  1582. MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
  1583. }
  1584. /// HandleDefineOtherTargetDirective - Implements #define_other_target.
  1585. void Preprocessor::HandleDefineOtherTargetDirective(LexerToken &Tok) {
  1586. LexerToken MacroNameTok;
  1587. ReadMacroName(MacroNameTok, 1);
  1588. // Error reading macro name? If so, diagnostic already issued.
  1589. if (MacroNameTok.getKind() == tok::eom)
  1590. return;
  1591. // Check to see if this is the last token on the #undef line.
  1592. CheckEndOfDirective("#define_other_target");
  1593. // If there is already a macro defined by this name, turn it into a
  1594. // target-specific define.
  1595. if (MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
  1596. MI->setIsTargetSpecific(true);
  1597. return;
  1598. }
  1599. // Mark the identifier as being a macro on some other target.
  1600. MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
  1601. }
  1602. /// HandleUndefDirective - Implements #undef.
  1603. ///
  1604. void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
  1605. ++NumUndefined;
  1606. LexerToken MacroNameTok;
  1607. ReadMacroName(MacroNameTok, 2);
  1608. // Error reading macro name? If so, diagnostic already issued.
  1609. if (MacroNameTok.getKind() == tok::eom)
  1610. return;
  1611. // Check to see if this is the last token on the #undef line.
  1612. CheckEndOfDirective("#undef");
  1613. // Okay, we finally have a valid identifier to undef.
  1614. MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
  1615. // #undef untaints an identifier if it were marked by define_other_target.
  1616. MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
  1617. // If the macro is not defined, this is a noop undef, just return.
  1618. if (MI == 0) return;
  1619. if (!MI->isUsed())
  1620. Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
  1621. // Free macro definition.
  1622. delete MI;
  1623. MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
  1624. }
  1625. //===----------------------------------------------------------------------===//
  1626. // Preprocessor Conditional Directive Handling.
  1627. //===----------------------------------------------------------------------===//
  1628. /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
  1629. /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
  1630. /// if any tokens have been returned or pp-directives activated before this
  1631. /// #ifndef has been lexed.
  1632. ///
  1633. void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
  1634. bool ReadAnyTokensBeforeDirective) {
  1635. ++NumIf;
  1636. LexerToken DirectiveTok = Result;
  1637. LexerToken MacroNameTok;
  1638. ReadMacroName(MacroNameTok);
  1639. // Error reading macro name? If so, diagnostic already issued.
  1640. if (MacroNameTok.getKind() == tok::eom)
  1641. return;
  1642. // Check to see if this is the last token on the #if[n]def line.
  1643. CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
  1644. // If the start of a top-level #ifdef, inform MIOpt.
  1645. if (!ReadAnyTokensBeforeDirective &&
  1646. CurLexer->getConditionalStackDepth() == 0) {
  1647. assert(isIfndef && "#ifdef shouldn't reach here");
  1648. CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
  1649. }
  1650. IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
  1651. MacroInfo *MI = MII->getMacroInfo();
  1652. // If there is a macro, process it.
  1653. if (MI) {
  1654. // Mark it used.
  1655. MI->setIsUsed(true);
  1656. // If this is the first use of a target-specific macro, warn about it.
  1657. if (MI->isTargetSpecific()) {
  1658. MI->setIsTargetSpecific(false); // Don't warn on second use.
  1659. getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
  1660. diag::port_target_macro_use);
  1661. }
  1662. } else {
  1663. // Use of a target-specific macro for some other target? If so, warn.
  1664. if (MII->isOtherTargetMacro()) {
  1665. MII->setIsOtherTargetMacro(false); // Don't warn on second use.
  1666. getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
  1667. diag::port_target_macro_use);
  1668. }
  1669. }
  1670. // Should we include the stuff contained by this directive?
  1671. if (!MI == isIfndef) {
  1672. // Yes, remember that we are inside a conditional, then lex the next token.
  1673. CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
  1674. /*foundnonskip*/true, /*foundelse*/false);
  1675. } else {
  1676. // No, skip the contents of this block and return the first token after it.
  1677. SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
  1678. /*Foundnonskip*/false,
  1679. /*FoundElse*/false);
  1680. }
  1681. }
  1682. /// HandleIfDirective - Implements the #if directive.
  1683. ///
  1684. void Preprocessor::HandleIfDirective(LexerToken &IfToken,
  1685. bool ReadAnyTokensBeforeDirective) {
  1686. ++NumIf;
  1687. // Parse and evaluation the conditional expression.
  1688. IdentifierInfo *IfNDefMacro = 0;
  1689. bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
  1690. // Should we include the stuff contained by this directive?
  1691. if (ConditionalTrue) {
  1692. // If this condition is equivalent to #ifndef X, and if this is the first
  1693. // directive seen, handle it for the multiple-include optimization.
  1694. if (!ReadAnyTokensBeforeDirective &&
  1695. CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
  1696. CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
  1697. // Yes, remember that we are inside a conditional, then lex the next token.
  1698. CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
  1699. /*foundnonskip*/true, /*foundelse*/false);
  1700. } else {
  1701. // No, skip the contents of this block and return the first token after it.
  1702. SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
  1703. /*FoundElse*/false);
  1704. }
  1705. }
  1706. /// HandleEndifDirective - Implements the #endif directive.
  1707. ///
  1708. void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
  1709. ++NumEndif;
  1710. // Check that this is the whole directive.
  1711. CheckEndOfDirective("#endif");
  1712. PPConditionalInfo CondInfo;
  1713. if (CurLexer->popConditionalLevel(CondInfo)) {
  1714. // No conditionals on the stack: this is an #endif without an #if.
  1715. return Diag(EndifToken, diag::err_pp_endif_without_if);
  1716. }
  1717. // If this the end of a top-level #endif, inform MIOpt.
  1718. if (CurLexer->getConditionalStackDepth() == 0)
  1719. CurLexer->MIOpt.ExitTopLevelConditional();
  1720. assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
  1721. "This code should only be reachable in the non-skipping case!");
  1722. }
  1723. void Preprocessor::HandleElseDirective(LexerToken &Result) {
  1724. ++NumElse;
  1725. // #else directive in a non-skipping conditional... start skipping.
  1726. CheckEndOfDirective("#else");
  1727. PPConditionalInfo CI;
  1728. if (CurLexer->popConditionalLevel(CI))
  1729. return Diag(Result, diag::pp_err_else_without_if);
  1730. // If this is a top-level #else, inform the MIOpt.
  1731. if (CurLexer->getConditionalStackDepth() == 0)
  1732. CurLexer->MIOpt.FoundTopLevelElse();
  1733. // If this is a #else with a #else before it, report the error.
  1734. if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
  1735. // Finally, skip the rest of the contents of this block and return the first
  1736. // token after it.
  1737. return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
  1738. /*FoundElse*/true);
  1739. }
  1740. void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
  1741. ++NumElse;
  1742. // #elif directive in a non-skipping conditional... start skipping.
  1743. // We don't care what the condition is, because we will always skip it (since
  1744. // the block immediately before it was included).
  1745. DiscardUntilEndOfDirective();
  1746. PPConditionalInfo CI;
  1747. if (CurLexer->popConditionalLevel(CI))
  1748. return Diag(ElifToken, diag::pp_err_elif_without_if);
  1749. // If this is a top-level #elif, inform the MIOpt.
  1750. if (CurLexer->getConditionalStackDepth() == 0)
  1751. CurLexer->MIOpt.FoundTopLevelElse();
  1752. // If this is a #elif with a #else before it, report the error.
  1753. if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
  1754. // Finally, skip the rest of the contents of this block and return the first
  1755. // token after it.
  1756. return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
  1757. /*FoundElse*/CI.FoundElse);
  1758. }