Preprocessor.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Preprocessor interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Options to support:
  15. // -H - Print the name of each header file used.
  16. // -d[DNI] - 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/Pragma.h"
  31. #include "clang/Lex/ScratchBuffer.h"
  32. #include "clang/Lex/LexDiagnostic.h"
  33. #include "clang/Basic/SourceManager.h"
  34. #include "clang/Basic/FileManager.h"
  35. #include "clang/Basic/TargetInfo.h"
  36. #include "llvm/ADT/APFloat.h"
  37. #include "llvm/ADT/SmallVector.h"
  38. #include "llvm/Support/MemoryBuffer.h"
  39. #include "llvm/Support/Streams.h"
  40. #include <cstdio>
  41. using namespace clang;
  42. //===----------------------------------------------------------------------===//
  43. PreprocessorFactory::~PreprocessorFactory() {}
  44. Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
  45. TargetInfo &target, SourceManager &SM,
  46. HeaderSearch &Headers,
  47. IdentifierInfoLookup* IILookup)
  48. : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
  49. SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts, IILookup),
  50. CurPPLexer(0), CurDirLookup(0), Callbacks(0) {
  51. ScratchBuf = new ScratchBuffer(SourceMgr);
  52. // Clear stats.
  53. NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
  54. NumIf = NumElse = NumEndif = 0;
  55. NumEnteredSourceFiles = 0;
  56. NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
  57. NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
  58. MaxIncludeStackDepth = 0;
  59. NumSkipped = 0;
  60. // Default to discarding comments.
  61. KeepComments = false;
  62. KeepMacroComments = false;
  63. // Macro expansion is enabled.
  64. DisableMacroExpansion = false;
  65. InMacroArgs = false;
  66. NumCachedTokenLexers = 0;
  67. CachedLexPos = 0;
  68. // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
  69. // This gets unpoisoned where it is allowed.
  70. (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
  71. // Initialize the pragma handlers.
  72. PragmaHandlers = new PragmaNamespace(0);
  73. RegisterBuiltinPragmas();
  74. // Initialize builtin macros like __LINE__ and friends.
  75. RegisterBuiltinMacros();
  76. }
  77. Preprocessor::~Preprocessor() {
  78. assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
  79. while (!IncludeMacroStack.empty()) {
  80. delete IncludeMacroStack.back().TheLexer;
  81. delete IncludeMacroStack.back().TheTokenLexer;
  82. IncludeMacroStack.pop_back();
  83. }
  84. // Free any macro definitions.
  85. for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
  86. Macros.begin(), E = Macros.end(); I != E; ++I) {
  87. // We don't need to free the MacroInfo objects directly. These
  88. // will be released when the BumpPtrAllocator 'BP' object gets
  89. // destroyed. We still need to run the dstor, however, to free
  90. // memory alocated by MacroInfo.
  91. I->second->Destroy(BP);
  92. I->first->setHasMacroDefinition(false);
  93. }
  94. // Free any cached macro expanders.
  95. for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
  96. delete TokenLexerCache[i];
  97. // Release pragma information.
  98. delete PragmaHandlers;
  99. // Delete the scratch buffer info.
  100. delete ScratchBuf;
  101. delete Callbacks;
  102. }
  103. void Preprocessor::setPTHManager(PTHManager* pm) {
  104. PTH.reset(pm);
  105. FileMgr.setStatCache(PTH->createStatCache());
  106. }
  107. void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
  108. llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
  109. << getSpelling(Tok) << "'";
  110. if (!DumpFlags) return;
  111. llvm::cerr << "\t";
  112. if (Tok.isAtStartOfLine())
  113. llvm::cerr << " [StartOfLine]";
  114. if (Tok.hasLeadingSpace())
  115. llvm::cerr << " [LeadingSpace]";
  116. if (Tok.isExpandDisabled())
  117. llvm::cerr << " [ExpandDisabled]";
  118. if (Tok.needsCleaning()) {
  119. const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
  120. llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
  121. << "']";
  122. }
  123. llvm::cerr << "\tLoc=<";
  124. DumpLocation(Tok.getLocation());
  125. llvm::cerr << ">";
  126. }
  127. void Preprocessor::DumpLocation(SourceLocation Loc) const {
  128. Loc.dump(SourceMgr);
  129. }
  130. void Preprocessor::DumpMacro(const MacroInfo &MI) const {
  131. llvm::cerr << "MACRO: ";
  132. for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
  133. DumpToken(MI.getReplacementToken(i));
  134. llvm::cerr << " ";
  135. }
  136. llvm::cerr << "\n";
  137. }
  138. void Preprocessor::PrintStats() {
  139. llvm::cerr << "\n*** Preprocessor Stats:\n";
  140. llvm::cerr << NumDirectives << " directives found:\n";
  141. llvm::cerr << " " << NumDefined << " #define.\n";
  142. llvm::cerr << " " << NumUndefined << " #undef.\n";
  143. llvm::cerr << " #include/#include_next/#import:\n";
  144. llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
  145. llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
  146. llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
  147. llvm::cerr << " " << NumElse << " #else/#elif.\n";
  148. llvm::cerr << " " << NumEndif << " #endif.\n";
  149. llvm::cerr << " " << NumPragma << " #pragma.\n";
  150. llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
  151. llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
  152. << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
  153. << NumFastMacroExpanded << " on the fast path.\n";
  154. llvm::cerr << (NumFastTokenPaste+NumTokenPaste)
  155. << " token paste (##) operations performed, "
  156. << NumFastTokenPaste << " on the fast path.\n";
  157. }
  158. //===----------------------------------------------------------------------===//
  159. // Token Spelling
  160. //===----------------------------------------------------------------------===//
  161. /// getSpelling() - Return the 'spelling' of this token. The spelling of a
  162. /// token are the characters used to represent the token in the source file
  163. /// after trigraph expansion and escaped-newline folding. In particular, this
  164. /// wants to get the true, uncanonicalized, spelling of things like digraphs
  165. /// UCNs, etc.
  166. std::string Preprocessor::getSpelling(const Token &Tok) const {
  167. assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
  168. // If this token contains nothing interesting, return it directly.
  169. const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation());
  170. if (!Tok.needsCleaning())
  171. return std::string(TokStart, TokStart+Tok.getLength());
  172. std::string Result;
  173. Result.reserve(Tok.getLength());
  174. // Otherwise, hard case, relex the characters into the string.
  175. for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
  176. Ptr != End; ) {
  177. unsigned CharSize;
  178. Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
  179. Ptr += CharSize;
  180. }
  181. assert(Result.size() != unsigned(Tok.getLength()) &&
  182. "NeedsCleaning flag set on something that didn't need cleaning!");
  183. return Result;
  184. }
  185. /// getSpelling - This method is used to get the spelling of a token into a
  186. /// preallocated buffer, instead of as an std::string. The caller is required
  187. /// to allocate enough space for the token, which is guaranteed to be at least
  188. /// Tok.getLength() bytes long. The actual length of the token is returned.
  189. ///
  190. /// Note that this method may do two possible things: it may either fill in
  191. /// the buffer specified with characters, or it may *change the input pointer*
  192. /// to point to a constant buffer with the data already in it (avoiding a
  193. /// copy). The caller is not allowed to modify the returned buffer pointer
  194. /// if an internal buffer is returned.
  195. unsigned Preprocessor::getSpelling(const Token &Tok,
  196. const char *&Buffer) const {
  197. assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
  198. // If this token is an identifier, just return the string from the identifier
  199. // table, which is very quick.
  200. if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
  201. Buffer = II->getName();
  202. return II->getLength();
  203. }
  204. // Otherwise, compute the start of the token in the input lexer buffer.
  205. const char *TokStart = 0;
  206. if (Tok.isLiteral())
  207. TokStart = Tok.getLiteralData();
  208. if (TokStart == 0)
  209. TokStart = SourceMgr.getCharacterData(Tok.getLocation());
  210. // If this token contains nothing interesting, return it directly.
  211. if (!Tok.needsCleaning()) {
  212. Buffer = TokStart;
  213. return Tok.getLength();
  214. }
  215. // Otherwise, hard case, relex the characters into the string.
  216. char *OutBuf = const_cast<char*>(Buffer);
  217. for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
  218. Ptr != End; ) {
  219. unsigned CharSize;
  220. *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
  221. Ptr += CharSize;
  222. }
  223. assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
  224. "NeedsCleaning flag set on something that didn't need cleaning!");
  225. return OutBuf-Buffer;
  226. }
  227. /// CreateString - Plop the specified string into a scratch buffer and return a
  228. /// location for it. If specified, the source location provides a source
  229. /// location for the token.
  230. void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok,
  231. SourceLocation InstantiationLoc) {
  232. Tok.setLength(Len);
  233. const char *DestPtr;
  234. SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr);
  235. if (InstantiationLoc.isValid())
  236. Loc = SourceMgr.createInstantiationLoc(Loc, InstantiationLoc,
  237. InstantiationLoc, Len);
  238. Tok.setLocation(Loc);
  239. // If this is a literal token, set the pointer data.
  240. if (Tok.isLiteral())
  241. Tok.setLiteralData(DestPtr);
  242. }
  243. /// AdvanceToTokenCharacter - Given a location that specifies the start of a
  244. /// token, return a new location that specifies a character within the token.
  245. SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
  246. unsigned CharNo) {
  247. // If they request the first char of the token, we're trivially done.
  248. if (CharNo == 0) return TokStart;
  249. // Figure out how many physical characters away the specified instantiation
  250. // character is. This needs to take into consideration newlines and
  251. // trigraphs.
  252. const char *TokPtr = SourceMgr.getCharacterData(TokStart);
  253. unsigned PhysOffset = 0;
  254. // The usual case is that tokens don't contain anything interesting. Skip
  255. // over the uninteresting characters. If a token only consists of simple
  256. // chars, this method is extremely fast.
  257. while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
  258. ++TokPtr, --CharNo, ++PhysOffset;
  259. // If we have a character that may be a trigraph or escaped newline, use a
  260. // lexer to parse it correctly.
  261. if (CharNo != 0) {
  262. // Skip over characters the remaining characters.
  263. for (; CharNo; --CharNo) {
  264. unsigned Size;
  265. Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features);
  266. TokPtr += Size;
  267. PhysOffset += Size;
  268. }
  269. }
  270. return TokStart.getFileLocWithOffset(PhysOffset);
  271. }
  272. /// \brief Computes the source location just past the end of the
  273. /// token at this source location.
  274. ///
  275. /// This routine can be used to produce a source location that
  276. /// points just past the end of the token referenced by \p Loc, and
  277. /// is generally used when a diagnostic needs to point just after a
  278. /// token where it expected something different that it received. If
  279. /// the returned source location would not be meaningful (e.g., if
  280. /// it points into a macro), this routine returns an invalid
  281. /// source location.
  282. SourceLocation Preprocessor::getLocForEndOfToken(SourceLocation Loc) {
  283. if (Loc.isInvalid() || !Loc.isFileID())
  284. return SourceLocation();
  285. unsigned Len = Lexer::MeasureTokenLength(Loc, getSourceManager());
  286. return AdvanceToTokenCharacter(Loc, Len);
  287. }
  288. //===----------------------------------------------------------------------===//
  289. // Preprocessor Initialization Methods
  290. //===----------------------------------------------------------------------===//
  291. // Append a #define line to Buf for Macro. Macro should be of the form XXX,
  292. // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
  293. // "#define XXX Y z W". To get a #define with no value, use "XXX=".
  294. static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
  295. const char *Command = "#define ") {
  296. Buf.insert(Buf.end(), Command, Command+strlen(Command));
  297. if (const char *Equal = strchr(Macro, '=')) {
  298. // Turn the = into ' '.
  299. Buf.insert(Buf.end(), Macro, Equal);
  300. Buf.push_back(' ');
  301. Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
  302. } else {
  303. // Push "macroname 1".
  304. Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
  305. Buf.push_back(' ');
  306. Buf.push_back('1');
  307. }
  308. Buf.push_back('\n');
  309. }
  310. /// PickFP - This is used to pick a value based on the FP semantics of the
  311. /// specified FP model.
  312. template <typename T>
  313. static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
  314. T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal) {
  315. if (Sem == &llvm::APFloat::IEEEsingle)
  316. return IEEESingleVal;
  317. if (Sem == &llvm::APFloat::IEEEdouble)
  318. return IEEEDoubleVal;
  319. if (Sem == &llvm::APFloat::x87DoubleExtended)
  320. return X87DoubleExtendedVal;
  321. assert(Sem == &llvm::APFloat::PPCDoubleDouble);
  322. return PPCDoubleDoubleVal;
  323. }
  324. static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix,
  325. const llvm::fltSemantics *Sem) {
  326. const char *DenormMin, *Epsilon, *Max, *Min;
  327. DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
  328. "3.64519953188247460253e-4951L",
  329. "4.94065645841246544176568792868221e-324L");
  330. int Digits = PickFP(Sem, 6, 15, 18, 31);
  331. Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
  332. "1.08420217248550443401e-19L",
  333. "4.94065645841246544176568792868221e-324L");
  334. int HasInifinity = 1, HasQuietNaN = 1;
  335. int MantissaDigits = PickFP(Sem, 24, 53, 64, 106);
  336. int Min10Exp = PickFP(Sem, -37, -307, -4931, -291);
  337. int Max10Exp = PickFP(Sem, 38, 308, 4932, 308);
  338. int MinExp = PickFP(Sem, -125, -1021, -16381, -968);
  339. int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024);
  340. Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
  341. "3.36210314311209350626e-4932L",
  342. "2.00416836000897277799610805135016e-292L");
  343. Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
  344. "1.18973149535723176502e+4932L",
  345. "1.79769313486231580793728971405301e+308L");
  346. char MacroBuf[60];
  347. sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin);
  348. DefineBuiltinMacro(Buf, MacroBuf);
  349. sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits);
  350. DefineBuiltinMacro(Buf, MacroBuf);
  351. sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon);
  352. DefineBuiltinMacro(Buf, MacroBuf);
  353. sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity);
  354. DefineBuiltinMacro(Buf, MacroBuf);
  355. sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN);
  356. DefineBuiltinMacro(Buf, MacroBuf);
  357. sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits);
  358. DefineBuiltinMacro(Buf, MacroBuf);
  359. sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp);
  360. DefineBuiltinMacro(Buf, MacroBuf);
  361. sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp);
  362. DefineBuiltinMacro(Buf, MacroBuf);
  363. sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max);
  364. DefineBuiltinMacro(Buf, MacroBuf);
  365. sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp);
  366. DefineBuiltinMacro(Buf, MacroBuf);
  367. sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp);
  368. DefineBuiltinMacro(Buf, MacroBuf);
  369. sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min);
  370. DefineBuiltinMacro(Buf, MacroBuf);
  371. sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix);
  372. DefineBuiltinMacro(Buf, MacroBuf);
  373. }
  374. /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
  375. /// named MacroName with the max value for a type with width 'TypeWidth' a
  376. /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
  377. static void DefineTypeSize(const char *MacroName, unsigned TypeWidth,
  378. const char *ValSuffix, bool isSigned,
  379. std::vector<char> &Buf) {
  380. char MacroBuf[60];
  381. uint64_t MaxVal;
  382. if (isSigned)
  383. MaxVal = (1LL << (TypeWidth - 1)) - 1;
  384. else
  385. MaxVal = ~0LL >> (64-TypeWidth);
  386. sprintf(MacroBuf, "%s=%llu%s", MacroName, MaxVal, ValSuffix);
  387. DefineBuiltinMacro(Buf, MacroBuf);
  388. }
  389. static void DefineType(const char *MacroName, TargetInfo::IntType Ty,
  390. std::vector<char> &Buf) {
  391. char MacroBuf[60];
  392. sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty));
  393. DefineBuiltinMacro(Buf, MacroBuf);
  394. }
  395. static void InitializePredefinedMacros(Preprocessor &PP,
  396. std::vector<char> &Buf) {
  397. char MacroBuf[60];
  398. // Compiler version introspection macros.
  399. DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
  400. DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
  401. // Currently claim to be compatible with GCC 4.2.1-5621.
  402. DefineBuiltinMacro(Buf, "__APPLE_CC__=5621");
  403. DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
  404. DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
  405. DefineBuiltinMacro(Buf, "__GNUC__=4");
  406. DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
  407. DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 (Apple Computer, Inc. "
  408. "build 5621) (dot 3)\"");
  409. // Initialize language-specific preprocessor defines.
  410. // These should all be defined in the preprocessor according to the
  411. // current language configuration.
  412. if (!PP.getLangOptions().Microsoft)
  413. DefineBuiltinMacro(Buf, "__STDC__=1");
  414. if (PP.getLangOptions().AsmPreprocessor)
  415. DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
  416. if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
  417. DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
  418. else if (0) // STDC94 ?
  419. DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
  420. DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
  421. if (PP.getLangOptions().ObjC1) {
  422. DefineBuiltinMacro(Buf, "__OBJC__=1");
  423. if (PP.getLangOptions().ObjCNonFragileABI)
  424. DefineBuiltinMacro(Buf, "__OBJC2__=1");
  425. if (PP.getLangOptions().getGCMode() == LangOptions::NonGC) {
  426. DefineBuiltinMacro(Buf, "__weak=");
  427. DefineBuiltinMacro(Buf, "__strong=");
  428. } else {
  429. DefineBuiltinMacro(Buf, "__weak=__attribute__((objc_gc(weak)))");
  430. DefineBuiltinMacro(Buf, "__strong=__attribute__((objc_gc(strong)))");
  431. DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
  432. }
  433. if (PP.getLangOptions().NeXTRuntime)
  434. DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
  435. }
  436. // darwin_constant_cfstrings controls this. This is also dependent
  437. // on other things like the runtime I believe. This is set even for C code.
  438. DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");
  439. if (PP.getLangOptions().ObjC2)
  440. DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
  441. if (PP.getLangOptions().PascalStrings)
  442. DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");
  443. if (PP.getLangOptions().Blocks) {
  444. DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
  445. DefineBuiltinMacro(Buf, "__BLOCKS__=1");
  446. }
  447. if (PP.getLangOptions().CPlusPlus) {
  448. DefineBuiltinMacro(Buf, "__DEPRECATED=1");
  449. DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
  450. DefineBuiltinMacro(Buf, "__GNUG__=4");
  451. DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
  452. DefineBuiltinMacro(Buf, "__cplusplus=1");
  453. DefineBuiltinMacro(Buf, "__private_extern__=extern");
  454. }
  455. // Filter out some microsoft extensions when trying to parse in ms-compat
  456. // mode.
  457. if (PP.getLangOptions().Microsoft) {
  458. DefineBuiltinMacro(Buf, "_cdecl=__cdecl");
  459. DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
  460. DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
  461. DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
  462. DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
  463. }
  464. // Initialize target-specific preprocessor defines.
  465. const TargetInfo &TI = PP.getTargetInfo();
  466. // Define type sizing macros based on the target properties.
  467. assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
  468. DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
  469. unsigned IntMaxWidth;
  470. const char *IntMaxSuffix;
  471. if (TI.getIntMaxType() == TargetInfo::SignedLongLong) {
  472. IntMaxWidth = TI.getLongLongWidth();
  473. IntMaxSuffix = "LL";
  474. } else if (TI.getIntMaxType() == TargetInfo::SignedLong) {
  475. IntMaxWidth = TI.getLongWidth();
  476. IntMaxSuffix = "L";
  477. } else {
  478. assert(TI.getIntMaxType() == TargetInfo::SignedInt);
  479. IntMaxWidth = TI.getIntWidth();
  480. IntMaxSuffix = "";
  481. }
  482. DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
  483. DefineTypeSize("__SHRT_MAX__", TI.getShortWidth(), "", true, Buf);
  484. DefineTypeSize("__INT_MAX__", TI.getIntWidth(), "", true, Buf);
  485. DefineTypeSize("__LONG_MAX__", TI.getLongWidth(), "L", true, Buf);
  486. DefineTypeSize("__LONG_LONG_MAX__", TI.getLongLongWidth(), "LL", true, Buf);
  487. DefineTypeSize("__WCHAR_MAX__", TI.getWCharWidth(), "", true, Buf);
  488. DefineTypeSize("__INTMAX_MAX__", IntMaxWidth, IntMaxSuffix, true, Buf);
  489. DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
  490. DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
  491. DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
  492. DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
  493. DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
  494. DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
  495. // FIXME: TargetInfo hookize __WINT_TYPE__.
  496. DefineBuiltinMacro(Buf, "__WINT_TYPE__=int");
  497. DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
  498. DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
  499. DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
  500. // Define a __POINTER_WIDTH__ macro for stdint.h.
  501. sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
  502. DefineBuiltinMacro(Buf, MacroBuf);
  503. if (!TI.isCharSigned())
  504. DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");
  505. // Define fixed-sized integer types for stdint.h
  506. assert(TI.getCharWidth() == 8 && "unsupported target types");
  507. assert(TI.getShortWidth() == 16 && "unsupported target types");
  508. DefineBuiltinMacro(Buf, "__INT8_TYPE__=char");
  509. DefineBuiltinMacro(Buf, "__INT16_TYPE__=short");
  510. if (TI.getIntWidth() == 32)
  511. DefineBuiltinMacro(Buf, "__INT32_TYPE__=int");
  512. else {
  513. assert(TI.getLongLongWidth() == 32 && "unsupported target types");
  514. DefineBuiltinMacro(Buf, "__INT32_TYPE__=long long");
  515. }
  516. // 16-bit targets doesn't necessarily have a 64-bit type.
  517. if (TI.getLongLongWidth() == 64)
  518. DefineBuiltinMacro(Buf, "__INT64_TYPE__=long long");
  519. // Add __builtin_va_list typedef.
  520. {
  521. const char *VAList = TI.getVAListDeclaration();
  522. Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
  523. Buf.push_back('\n');
  524. }
  525. if (const char *Prefix = TI.getUserLabelPrefix()) {
  526. sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
  527. DefineBuiltinMacro(Buf, MacroBuf);
  528. }
  529. // Build configuration options. FIXME: these should be controlled by
  530. // command line options or something.
  531. DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
  532. DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
  533. DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
  534. DefineBuiltinMacro(Buf, "__PIC__=1");
  535. // Macros to control C99 numerics and <float.h>
  536. DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
  537. DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
  538. sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
  539. PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33));
  540. DefineBuiltinMacro(Buf, MacroBuf);
  541. // Get other target #defines.
  542. TI.getTargetDefines(Buf);
  543. // FIXME: Should emit a #line directive here.
  544. }
  545. /// EnterMainSourceFile - Enter the specified FileID as the main source file,
  546. /// which implicitly adds the builtin defines etc.
  547. void Preprocessor::EnterMainSourceFile() {
  548. // We do not allow the preprocessor to reenter the main file. Doing so will
  549. // cause FileID's to accumulate information from both runs (e.g. #line
  550. // information) and predefined macros aren't guaranteed to be set properly.
  551. assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
  552. FileID MainFileID = SourceMgr.getMainFileID();
  553. // Enter the main file source buffer.
  554. EnterSourceFile(MainFileID, 0);
  555. // Tell the header info that the main file was entered. If the file is later
  556. // #imported, it won't be re-entered.
  557. if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
  558. HeaderInfo.IncrementIncludeCount(FE);
  559. std::vector<char> PrologFile;
  560. PrologFile.reserve(4080);
  561. // Install things like __POWERPC__, __GNUC__, etc into the macro table.
  562. InitializePredefinedMacros(*this, PrologFile);
  563. // Add on the predefines from the driver.
  564. PrologFile.insert(PrologFile.end(), Predefines.begin(), Predefines.end());
  565. // Memory buffer must end with a null byte!
  566. PrologFile.push_back(0);
  567. // Now that we have emitted the predefined macros, #includes, etc into
  568. // PrologFile, preprocess it to populate the initial preprocessor state.
  569. llvm::MemoryBuffer *SB =
  570. llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
  571. "<predefines>");
  572. assert(SB && "Cannot fail to create predefined source buffer");
  573. FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
  574. assert(!FID.isInvalid() && "Could not create FileID for predefines?");
  575. // Start parsing the predefines.
  576. EnterSourceFile(FID, 0);
  577. }
  578. //===----------------------------------------------------------------------===//
  579. // Lexer Event Handling.
  580. //===----------------------------------------------------------------------===//
  581. /// LookUpIdentifierInfo - Given a tok::identifier token, look up the
  582. /// identifier information for the token and install it into the token.
  583. IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
  584. const char *BufPtr) {
  585. assert(Identifier.is(tok::identifier) && "Not an identifier!");
  586. assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
  587. // Look up this token, see if it is a macro, or if it is a language keyword.
  588. IdentifierInfo *II;
  589. if (BufPtr && !Identifier.needsCleaning()) {
  590. // No cleaning needed, just use the characters from the lexed buffer.
  591. II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
  592. } else {
  593. // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
  594. llvm::SmallVector<char, 64> IdentifierBuffer;
  595. IdentifierBuffer.resize(Identifier.getLength());
  596. const char *TmpBuf = &IdentifierBuffer[0];
  597. unsigned Size = getSpelling(Identifier, TmpBuf);
  598. II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
  599. }
  600. Identifier.setIdentifierInfo(II);
  601. return II;
  602. }
  603. /// HandleIdentifier - This callback is invoked when the lexer reads an
  604. /// identifier. This callback looks up the identifier in the map and/or
  605. /// potentially macro expands it or turns it into a named token (like 'for').
  606. ///
  607. /// Note that callers of this method are guarded by checking the
  608. /// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the
  609. /// IdentifierInfo methods that compute these properties will need to change to
  610. /// match.
  611. void Preprocessor::HandleIdentifier(Token &Identifier) {
  612. assert(Identifier.getIdentifierInfo() &&
  613. "Can't handle identifiers without identifier info!");
  614. IdentifierInfo &II = *Identifier.getIdentifierInfo();
  615. // If this identifier was poisoned, and if it was not produced from a macro
  616. // expansion, emit an error.
  617. if (II.isPoisoned() && CurPPLexer) {
  618. if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
  619. Diag(Identifier, diag::err_pp_used_poisoned_id);
  620. else
  621. Diag(Identifier, diag::ext_pp_bad_vaargs_use);
  622. }
  623. // If this is a macro to be expanded, do it.
  624. if (MacroInfo *MI = getMacroInfo(&II)) {
  625. if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
  626. if (MI->isEnabled()) {
  627. if (!HandleMacroExpandedIdentifier(Identifier, MI))
  628. return;
  629. } else {
  630. // C99 6.10.3.4p2 says that a disabled macro may never again be
  631. // expanded, even if it's in a context where it could be expanded in the
  632. // future.
  633. Identifier.setFlag(Token::DisableExpand);
  634. }
  635. }
  636. }
  637. // C++ 2.11p2: If this is an alternative representation of a C++ operator,
  638. // then we act as if it is the actual operator and not the textual
  639. // representation of it.
  640. if (II.isCPlusPlusOperatorKeyword())
  641. Identifier.setIdentifierInfo(0);
  642. // If this is an extension token, diagnose its use.
  643. // We avoid diagnosing tokens that originate from macro definitions.
  644. if (II.isExtensionToken() && Features.C99 && !DisableMacroExpansion)
  645. Diag(Identifier, diag::ext_token_used);
  646. }