PPExpressions.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. //===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the Preprocessor::EvaluateDirectiveExpression method,
  10. // which parses and evaluates integer constant expressions for #if directives.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // FIXME: implement testing for #assert's.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "clang/Basic/IdentifierTable.h"
  18. #include "clang/Basic/SourceLocation.h"
  19. #include "clang/Basic/SourceManager.h"
  20. #include "clang/Basic/TargetInfo.h"
  21. #include "clang/Basic/TokenKinds.h"
  22. #include "clang/Lex/CodeCompletionHandler.h"
  23. #include "clang/Lex/LexDiagnostic.h"
  24. #include "clang/Lex/LiteralSupport.h"
  25. #include "clang/Lex/MacroInfo.h"
  26. #include "clang/Lex/PPCallbacks.h"
  27. #include "clang/Lex/Preprocessor.h"
  28. #include "clang/Lex/Token.h"
  29. #include "llvm/ADT/APSInt.h"
  30. #include "llvm/ADT/STLExtras.h"
  31. #include "llvm/ADT/SmallString.h"
  32. #include "llvm/ADT/StringExtras.h"
  33. #include "llvm/ADT/StringRef.h"
  34. #include "llvm/Support/ErrorHandling.h"
  35. #include "llvm/Support/SaveAndRestore.h"
  36. #include <cassert>
  37. using namespace clang;
  38. namespace {
  39. /// PPValue - Represents the value of a subexpression of a preprocessor
  40. /// conditional and the source range covered by it.
  41. class PPValue {
  42. SourceRange Range;
  43. IdentifierInfo *II;
  44. public:
  45. llvm::APSInt Val;
  46. // Default ctor - Construct an 'invalid' PPValue.
  47. PPValue(unsigned BitWidth) : Val(BitWidth) {}
  48. // If this value was produced by directly evaluating an identifier, produce
  49. // that identifier.
  50. IdentifierInfo *getIdentifier() const { return II; }
  51. void setIdentifier(IdentifierInfo *II) { this->II = II; }
  52. unsigned getBitWidth() const { return Val.getBitWidth(); }
  53. bool isUnsigned() const { return Val.isUnsigned(); }
  54. SourceRange getRange() const { return Range; }
  55. void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
  56. void setRange(SourceLocation B, SourceLocation E) {
  57. Range.setBegin(B); Range.setEnd(E);
  58. }
  59. void setBegin(SourceLocation L) { Range.setBegin(L); }
  60. void setEnd(SourceLocation L) { Range.setEnd(L); }
  61. };
  62. } // end anonymous namespace
  63. static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
  64. Token &PeekTok, bool ValueLive,
  65. bool &IncludedUndefinedIds,
  66. Preprocessor &PP);
  67. /// DefinedTracker - This struct is used while parsing expressions to keep track
  68. /// of whether !defined(X) has been seen.
  69. ///
  70. /// With this simple scheme, we handle the basic forms:
  71. /// !defined(X) and !defined X
  72. /// but we also trivially handle (silly) stuff like:
  73. /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
  74. struct DefinedTracker {
  75. /// Each time a Value is evaluated, it returns information about whether the
  76. /// parsed value is of the form defined(X), !defined(X) or is something else.
  77. enum TrackerState {
  78. DefinedMacro, // defined(X)
  79. NotDefinedMacro, // !defined(X)
  80. Unknown // Something else.
  81. } State;
  82. /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
  83. /// indicates the macro that was checked.
  84. IdentifierInfo *TheMacro;
  85. bool IncludedUndefinedIds = false;
  86. };
  87. /// EvaluateDefined - Process a 'defined(sym)' expression.
  88. static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
  89. bool ValueLive, Preprocessor &PP) {
  90. SourceLocation beginLoc(PeekTok.getLocation());
  91. Result.setBegin(beginLoc);
  92. // Get the next token, don't expand it.
  93. PP.LexUnexpandedNonComment(PeekTok);
  94. // Two options, it can either be a pp-identifier or a (.
  95. SourceLocation LParenLoc;
  96. if (PeekTok.is(tok::l_paren)) {
  97. // Found a paren, remember we saw it and skip it.
  98. LParenLoc = PeekTok.getLocation();
  99. PP.LexUnexpandedNonComment(PeekTok);
  100. }
  101. if (PeekTok.is(tok::code_completion)) {
  102. if (PP.getCodeCompletionHandler())
  103. PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
  104. PP.setCodeCompletionReached();
  105. PP.LexUnexpandedNonComment(PeekTok);
  106. }
  107. // If we don't have a pp-identifier now, this is an error.
  108. if (PP.CheckMacroName(PeekTok, MU_Other))
  109. return true;
  110. // Otherwise, we got an identifier, is it defined to something?
  111. IdentifierInfo *II = PeekTok.getIdentifierInfo();
  112. MacroDefinition Macro = PP.getMacroDefinition(II);
  113. Result.Val = !!Macro;
  114. Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
  115. DT.IncludedUndefinedIds = !Macro;
  116. PP.emitMacroExpansionWarnings(PeekTok);
  117. // If there is a macro, mark it used.
  118. if (Result.Val != 0 && ValueLive)
  119. PP.markMacroAsUsed(Macro.getMacroInfo());
  120. // Save macro token for callback.
  121. Token macroToken(PeekTok);
  122. // If we are in parens, ensure we have a trailing ).
  123. if (LParenLoc.isValid()) {
  124. // Consume identifier.
  125. Result.setEnd(PeekTok.getLocation());
  126. PP.LexUnexpandedNonComment(PeekTok);
  127. if (PeekTok.isNot(tok::r_paren)) {
  128. PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
  129. << "'defined'" << tok::r_paren;
  130. PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
  131. return true;
  132. }
  133. // Consume the ).
  134. PP.LexNonComment(PeekTok);
  135. Result.setEnd(PeekTok.getLocation());
  136. } else {
  137. // Consume identifier.
  138. Result.setEnd(PeekTok.getLocation());
  139. PP.LexNonComment(PeekTok);
  140. }
  141. // [cpp.cond]p4:
  142. // Prior to evaluation, macro invocations in the list of preprocessing
  143. // tokens that will become the controlling constant expression are replaced
  144. // (except for those macro names modified by the 'defined' unary operator),
  145. // just as in normal text. If the token 'defined' is generated as a result
  146. // of this replacement process or use of the 'defined' unary operator does
  147. // not match one of the two specified forms prior to macro replacement, the
  148. // behavior is undefined.
  149. // This isn't an idle threat, consider this program:
  150. // #define FOO
  151. // #define BAR defined(FOO)
  152. // #if BAR
  153. // ...
  154. // #else
  155. // ...
  156. // #endif
  157. // clang and gcc will pick the #if branch while Visual Studio will take the
  158. // #else branch. Emit a warning about this undefined behavior.
  159. if (beginLoc.isMacroID()) {
  160. bool IsFunctionTypeMacro =
  161. PP.getSourceManager()
  162. .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
  163. .getExpansion()
  164. .isFunctionMacroExpansion();
  165. // For object-type macros, it's easy to replace
  166. // #define FOO defined(BAR)
  167. // with
  168. // #if defined(BAR)
  169. // #define FOO 1
  170. // #else
  171. // #define FOO 0
  172. // #endif
  173. // and doing so makes sense since compilers handle this differently in
  174. // practice (see example further up). But for function-type macros,
  175. // there is no good way to write
  176. // # define FOO(x) (defined(M_ ## x) && M_ ## x)
  177. // in a different way, and compilers seem to agree on how to behave here.
  178. // So warn by default on object-type macros, but only warn in -pedantic
  179. // mode on function-type macros.
  180. if (IsFunctionTypeMacro)
  181. PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
  182. else
  183. PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
  184. }
  185. // Invoke the 'defined' callback.
  186. if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
  187. Callbacks->Defined(macroToken, Macro,
  188. SourceRange(beginLoc, PeekTok.getLocation()));
  189. }
  190. // Success, remember that we saw defined(X).
  191. DT.State = DefinedTracker::DefinedMacro;
  192. DT.TheMacro = II;
  193. return false;
  194. }
  195. /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
  196. /// return the computed value in Result. Return true if there was an error
  197. /// parsing. This function also returns information about the form of the
  198. /// expression in DT. See above for information on what DT means.
  199. ///
  200. /// If ValueLive is false, then this value is being evaluated in a context where
  201. /// the result is not used. As such, avoid diagnostics that relate to
  202. /// evaluation.
  203. static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
  204. bool ValueLive, Preprocessor &PP) {
  205. DT.State = DefinedTracker::Unknown;
  206. Result.setIdentifier(nullptr);
  207. if (PeekTok.is(tok::code_completion)) {
  208. if (PP.getCodeCompletionHandler())
  209. PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
  210. PP.setCodeCompletionReached();
  211. PP.LexNonComment(PeekTok);
  212. }
  213. switch (PeekTok.getKind()) {
  214. default:
  215. // If this token's spelling is a pp-identifier, check to see if it is
  216. // 'defined' or if it is a macro. Note that we check here because many
  217. // keywords are pp-identifiers, so we can't check the kind.
  218. if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
  219. // Handle "defined X" and "defined(X)".
  220. if (II->isStr("defined"))
  221. return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
  222. if (!II->isCPlusPlusOperatorKeyword()) {
  223. // If this identifier isn't 'defined' or one of the special
  224. // preprocessor keywords and it wasn't macro expanded, it turns
  225. // into a simple 0
  226. if (ValueLive) {
  227. PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
  228. const DiagnosticsEngine &DiagEngine = PP.getDiagnostics();
  229. // If 'Wundef' is enabled, do not emit 'undef-prefix' diagnostics.
  230. if (DiagEngine.isIgnored(diag::warn_pp_undef_identifier,
  231. PeekTok.getLocation())) {
  232. const std::vector<std::string> UndefPrefixes =
  233. DiagEngine.getDiagnosticOptions().UndefPrefixes;
  234. const StringRef IdentifierName = II->getName();
  235. if (llvm::any_of(UndefPrefixes,
  236. [&IdentifierName](const std::string &Prefix) {
  237. return IdentifierName.startswith(Prefix);
  238. }))
  239. PP.Diag(PeekTok, diag::warn_pp_undef_prefix)
  240. << AddFlagValue{llvm::join(UndefPrefixes, ",")} << II;
  241. }
  242. }
  243. Result.Val = 0;
  244. Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
  245. Result.setIdentifier(II);
  246. Result.setRange(PeekTok.getLocation());
  247. DT.IncludedUndefinedIds = true;
  248. PP.LexNonComment(PeekTok);
  249. return false;
  250. }
  251. }
  252. PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
  253. return true;
  254. case tok::eod:
  255. case tok::r_paren:
  256. // If there is no expression, report and exit.
  257. PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
  258. return true;
  259. case tok::numeric_constant: {
  260. SmallString<64> IntegerBuffer;
  261. bool NumberInvalid = false;
  262. StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
  263. &NumberInvalid);
  264. if (NumberInvalid)
  265. return true; // a diagnostic was already reported
  266. NumericLiteralParser Literal(Spelling, PeekTok.getLocation(),
  267. PP.getSourceManager(), PP.getLangOpts(),
  268. PP.getTargetInfo(), PP.getDiagnostics());
  269. if (Literal.hadError)
  270. return true; // a diagnostic was already reported.
  271. if (Literal.isFloatingLiteral() || Literal.isImaginary) {
  272. PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
  273. return true;
  274. }
  275. assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
  276. // Complain about, and drop, any ud-suffix.
  277. if (Literal.hasUDSuffix())
  278. PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
  279. // 'long long' is a C99 or C++11 feature.
  280. if (!PP.getLangOpts().C99 && Literal.isLongLong) {
  281. if (PP.getLangOpts().CPlusPlus)
  282. PP.Diag(PeekTok,
  283. PP.getLangOpts().CPlusPlus11 ?
  284. diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
  285. else
  286. PP.Diag(PeekTok, diag::ext_c99_longlong);
  287. }
  288. // 'z/uz' literals are a C++2b feature.
  289. if (Literal.isSizeT)
  290. PP.Diag(PeekTok, PP.getLangOpts().CPlusPlus
  291. ? PP.getLangOpts().CPlusPlus2b
  292. ? diag::warn_cxx20_compat_size_t_suffix
  293. : diag::ext_cxx2b_size_t_suffix
  294. : diag::err_cxx2b_size_t_suffix);
  295. // Parse the integer literal into Result.
  296. if (Literal.GetIntegerValue(Result.Val)) {
  297. // Overflow parsing integer literal.
  298. if (ValueLive)
  299. PP.Diag(PeekTok, diag::err_integer_literal_too_large)
  300. << /* Unsigned */ 1;
  301. Result.Val.setIsUnsigned(true);
  302. } else {
  303. // Set the signedness of the result to match whether there was a U suffix
  304. // or not.
  305. Result.Val.setIsUnsigned(Literal.isUnsigned);
  306. // Detect overflow based on whether the value is signed. If signed
  307. // and if the value is too large, emit a warning "integer constant is so
  308. // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
  309. // is 64-bits.
  310. if (!Literal.isUnsigned && Result.Val.isNegative()) {
  311. // Octal, hexadecimal, and binary literals are implicitly unsigned if
  312. // the value does not fit into a signed integer type.
  313. if (ValueLive && Literal.getRadix() == 10)
  314. PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
  315. Result.Val.setIsUnsigned(true);
  316. }
  317. }
  318. // Consume the token.
  319. Result.setRange(PeekTok.getLocation());
  320. PP.LexNonComment(PeekTok);
  321. return false;
  322. }
  323. case tok::char_constant: // 'x'
  324. case tok::wide_char_constant: // L'x'
  325. case tok::utf8_char_constant: // u8'x'
  326. case tok::utf16_char_constant: // u'x'
  327. case tok::utf32_char_constant: { // U'x'
  328. // Complain about, and drop, any ud-suffix.
  329. if (PeekTok.hasUDSuffix())
  330. PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
  331. SmallString<32> CharBuffer;
  332. bool CharInvalid = false;
  333. StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
  334. if (CharInvalid)
  335. return true;
  336. CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
  337. PeekTok.getLocation(), PP, PeekTok.getKind());
  338. if (Literal.hadError())
  339. return true; // A diagnostic was already emitted.
  340. // Character literals are always int or wchar_t, expand to intmax_t.
  341. const TargetInfo &TI = PP.getTargetInfo();
  342. unsigned NumBits;
  343. if (Literal.isMultiChar())
  344. NumBits = TI.getIntWidth();
  345. else if (Literal.isWide())
  346. NumBits = TI.getWCharWidth();
  347. else if (Literal.isUTF16())
  348. NumBits = TI.getChar16Width();
  349. else if (Literal.isUTF32())
  350. NumBits = TI.getChar32Width();
  351. else // char or char8_t
  352. NumBits = TI.getCharWidth();
  353. // Set the width.
  354. llvm::APSInt Val(NumBits);
  355. // Set the value.
  356. Val = Literal.getValue();
  357. // Set the signedness. UTF-16 and UTF-32 are always unsigned
  358. if (Literal.isWide())
  359. Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
  360. else if (!Literal.isUTF16() && !Literal.isUTF32())
  361. Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
  362. if (Result.Val.getBitWidth() > Val.getBitWidth()) {
  363. Result.Val = Val.extend(Result.Val.getBitWidth());
  364. } else {
  365. assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
  366. "intmax_t smaller than char/wchar_t?");
  367. Result.Val = Val;
  368. }
  369. // Consume the token.
  370. Result.setRange(PeekTok.getLocation());
  371. PP.LexNonComment(PeekTok);
  372. return false;
  373. }
  374. case tok::l_paren: {
  375. SourceLocation Start = PeekTok.getLocation();
  376. PP.LexNonComment(PeekTok); // Eat the (.
  377. // Parse the value and if there are any binary operators involved, parse
  378. // them.
  379. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  380. // If this is a silly value like (X), which doesn't need parens, check for
  381. // !(defined X).
  382. if (PeekTok.is(tok::r_paren)) {
  383. // Just use DT unmodified as our result.
  384. } else {
  385. // Otherwise, we have something like (x+y), and we consumed '(x'.
  386. if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
  387. DT.IncludedUndefinedIds, PP))
  388. return true;
  389. if (PeekTok.isNot(tok::r_paren)) {
  390. PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
  391. << Result.getRange();
  392. PP.Diag(Start, diag::note_matching) << tok::l_paren;
  393. return true;
  394. }
  395. DT.State = DefinedTracker::Unknown;
  396. }
  397. Result.setRange(Start, PeekTok.getLocation());
  398. Result.setIdentifier(nullptr);
  399. PP.LexNonComment(PeekTok); // Eat the ).
  400. return false;
  401. }
  402. case tok::plus: {
  403. SourceLocation Start = PeekTok.getLocation();
  404. // Unary plus doesn't modify the value.
  405. PP.LexNonComment(PeekTok);
  406. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  407. Result.setBegin(Start);
  408. Result.setIdentifier(nullptr);
  409. return false;
  410. }
  411. case tok::minus: {
  412. SourceLocation Loc = PeekTok.getLocation();
  413. PP.LexNonComment(PeekTok);
  414. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  415. Result.setBegin(Loc);
  416. Result.setIdentifier(nullptr);
  417. // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
  418. Result.Val = -Result.Val;
  419. // -MININT is the only thing that overflows. Unsigned never overflows.
  420. bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
  421. // If this operator is live and overflowed, report the issue.
  422. if (Overflow && ValueLive)
  423. PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
  424. DT.State = DefinedTracker::Unknown;
  425. return false;
  426. }
  427. case tok::tilde: {
  428. SourceLocation Start = PeekTok.getLocation();
  429. PP.LexNonComment(PeekTok);
  430. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  431. Result.setBegin(Start);
  432. Result.setIdentifier(nullptr);
  433. // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
  434. Result.Val = ~Result.Val;
  435. DT.State = DefinedTracker::Unknown;
  436. return false;
  437. }
  438. case tok::exclaim: {
  439. SourceLocation Start = PeekTok.getLocation();
  440. PP.LexNonComment(PeekTok);
  441. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  442. Result.setBegin(Start);
  443. Result.Val = !Result.Val;
  444. // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
  445. Result.Val.setIsUnsigned(false);
  446. Result.setIdentifier(nullptr);
  447. if (DT.State == DefinedTracker::DefinedMacro)
  448. DT.State = DefinedTracker::NotDefinedMacro;
  449. else if (DT.State == DefinedTracker::NotDefinedMacro)
  450. DT.State = DefinedTracker::DefinedMacro;
  451. return false;
  452. }
  453. case tok::kw_true:
  454. case tok::kw_false:
  455. Result.Val = PeekTok.getKind() == tok::kw_true;
  456. Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
  457. Result.setIdentifier(PeekTok.getIdentifierInfo());
  458. Result.setRange(PeekTok.getLocation());
  459. PP.LexNonComment(PeekTok);
  460. return false;
  461. // FIXME: Handle #assert
  462. }
  463. }
  464. /// getPrecedence - Return the precedence of the specified binary operator
  465. /// token. This returns:
  466. /// ~0 - Invalid token.
  467. /// 14 -> 3 - various operators.
  468. /// 0 - 'eod' or ')'
  469. static unsigned getPrecedence(tok::TokenKind Kind) {
  470. switch (Kind) {
  471. default: return ~0U;
  472. case tok::percent:
  473. case tok::slash:
  474. case tok::star: return 14;
  475. case tok::plus:
  476. case tok::minus: return 13;
  477. case tok::lessless:
  478. case tok::greatergreater: return 12;
  479. case tok::lessequal:
  480. case tok::less:
  481. case tok::greaterequal:
  482. case tok::greater: return 11;
  483. case tok::exclaimequal:
  484. case tok::equalequal: return 10;
  485. case tok::amp: return 9;
  486. case tok::caret: return 8;
  487. case tok::pipe: return 7;
  488. case tok::ampamp: return 6;
  489. case tok::pipepipe: return 5;
  490. case tok::question: return 4;
  491. case tok::comma: return 3;
  492. case tok::colon: return 2;
  493. case tok::r_paren: return 0;// Lowest priority, end of expr.
  494. case tok::eod: return 0;// Lowest priority, end of directive.
  495. }
  496. }
  497. static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
  498. Token &Tok) {
  499. if (Tok.is(tok::l_paren) && LHS.getIdentifier())
  500. PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
  501. << LHS.getIdentifier();
  502. else
  503. PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
  504. << LHS.getRange();
  505. }
  506. /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
  507. /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
  508. ///
  509. /// If ValueLive is false, then this value is being evaluated in a context where
  510. /// the result is not used. As such, avoid diagnostics that relate to
  511. /// evaluation, such as division by zero warnings.
  512. static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
  513. Token &PeekTok, bool ValueLive,
  514. bool &IncludedUndefinedIds,
  515. Preprocessor &PP) {
  516. unsigned PeekPrec = getPrecedence(PeekTok.getKind());
  517. // If this token isn't valid, report the error.
  518. if (PeekPrec == ~0U) {
  519. diagnoseUnexpectedOperator(PP, LHS, PeekTok);
  520. return true;
  521. }
  522. while (true) {
  523. // If this token has a lower precedence than we are allowed to parse, return
  524. // it so that higher levels of the recursion can parse it.
  525. if (PeekPrec < MinPrec)
  526. return false;
  527. tok::TokenKind Operator = PeekTok.getKind();
  528. // If this is a short-circuiting operator, see if the RHS of the operator is
  529. // dead. Note that this cannot just clobber ValueLive. Consider
  530. // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
  531. // this example, the RHS of the && being dead does not make the rest of the
  532. // expr dead.
  533. bool RHSIsLive;
  534. if (Operator == tok::ampamp && LHS.Val == 0)
  535. RHSIsLive = false; // RHS of "0 && x" is dead.
  536. else if (Operator == tok::pipepipe && LHS.Val != 0)
  537. RHSIsLive = false; // RHS of "1 || x" is dead.
  538. else if (Operator == tok::question && LHS.Val == 0)
  539. RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
  540. else
  541. RHSIsLive = ValueLive;
  542. // Consume the operator, remembering the operator's location for reporting.
  543. SourceLocation OpLoc = PeekTok.getLocation();
  544. PP.LexNonComment(PeekTok);
  545. PPValue RHS(LHS.getBitWidth());
  546. // Parse the RHS of the operator.
  547. DefinedTracker DT;
  548. if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
  549. IncludedUndefinedIds = DT.IncludedUndefinedIds;
  550. // Remember the precedence of this operator and get the precedence of the
  551. // operator immediately to the right of the RHS.
  552. unsigned ThisPrec = PeekPrec;
  553. PeekPrec = getPrecedence(PeekTok.getKind());
  554. // If this token isn't valid, report the error.
  555. if (PeekPrec == ~0U) {
  556. diagnoseUnexpectedOperator(PP, RHS, PeekTok);
  557. return true;
  558. }
  559. // Decide whether to include the next binop in this subexpression. For
  560. // example, when parsing x+y*z and looking at '*', we want to recursively
  561. // handle y*z as a single subexpression. We do this because the precedence
  562. // of * is higher than that of +. The only strange case we have to handle
  563. // here is for the ?: operator, where the precedence is actually lower than
  564. // the LHS of the '?'. The grammar rule is:
  565. //
  566. // conditional-expression ::=
  567. // logical-OR-expression ? expression : conditional-expression
  568. // where 'expression' is actually comma-expression.
  569. unsigned RHSPrec;
  570. if (Operator == tok::question)
  571. // The RHS of "?" should be maximally consumed as an expression.
  572. RHSPrec = getPrecedence(tok::comma);
  573. else // All others should munch while higher precedence.
  574. RHSPrec = ThisPrec+1;
  575. if (PeekPrec >= RHSPrec) {
  576. if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
  577. IncludedUndefinedIds, PP))
  578. return true;
  579. PeekPrec = getPrecedence(PeekTok.getKind());
  580. }
  581. assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
  582. // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
  583. // either operand is unsigned.
  584. llvm::APSInt Res(LHS.getBitWidth());
  585. switch (Operator) {
  586. case tok::question: // No UAC for x and y in "x ? y : z".
  587. case tok::lessless: // Shift amount doesn't UAC with shift value.
  588. case tok::greatergreater: // Shift amount doesn't UAC with shift value.
  589. case tok::comma: // Comma operands are not subject to UACs.
  590. case tok::pipepipe: // Logical || does not do UACs.
  591. case tok::ampamp: // Logical && does not do UACs.
  592. break; // No UAC
  593. default:
  594. Res.setIsUnsigned(LHS.isUnsigned() || RHS.isUnsigned());
  595. // If this just promoted something from signed to unsigned, and if the
  596. // value was negative, warn about it.
  597. if (ValueLive && Res.isUnsigned()) {
  598. if (!LHS.isUnsigned() && LHS.Val.isNegative())
  599. PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
  600. << toString(LHS.Val, 10, true) + " to " +
  601. toString(LHS.Val, 10, false)
  602. << LHS.getRange() << RHS.getRange();
  603. if (!RHS.isUnsigned() && RHS.Val.isNegative())
  604. PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
  605. << toString(RHS.Val, 10, true) + " to " +
  606. toString(RHS.Val, 10, false)
  607. << LHS.getRange() << RHS.getRange();
  608. }
  609. LHS.Val.setIsUnsigned(Res.isUnsigned());
  610. RHS.Val.setIsUnsigned(Res.isUnsigned());
  611. }
  612. bool Overflow = false;
  613. switch (Operator) {
  614. default: llvm_unreachable("Unknown operator token!");
  615. case tok::percent:
  616. if (RHS.Val != 0)
  617. Res = LHS.Val % RHS.Val;
  618. else if (ValueLive) {
  619. PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
  620. << LHS.getRange() << RHS.getRange();
  621. return true;
  622. }
  623. break;
  624. case tok::slash:
  625. if (RHS.Val != 0) {
  626. if (LHS.Val.isSigned())
  627. Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
  628. else
  629. Res = LHS.Val / RHS.Val;
  630. } else if (ValueLive) {
  631. PP.Diag(OpLoc, diag::err_pp_division_by_zero)
  632. << LHS.getRange() << RHS.getRange();
  633. return true;
  634. }
  635. break;
  636. case tok::star:
  637. if (Res.isSigned())
  638. Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
  639. else
  640. Res = LHS.Val * RHS.Val;
  641. break;
  642. case tok::lessless: {
  643. // Determine whether overflow is about to happen.
  644. if (LHS.isUnsigned())
  645. Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
  646. else
  647. Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
  648. break;
  649. }
  650. case tok::greatergreater: {
  651. // Determine whether overflow is about to happen.
  652. unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
  653. if (ShAmt >= LHS.getBitWidth()) {
  654. Overflow = true;
  655. ShAmt = LHS.getBitWidth()-1;
  656. }
  657. Res = LHS.Val >> ShAmt;
  658. break;
  659. }
  660. case tok::plus:
  661. if (LHS.isUnsigned())
  662. Res = LHS.Val + RHS.Val;
  663. else
  664. Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
  665. break;
  666. case tok::minus:
  667. if (LHS.isUnsigned())
  668. Res = LHS.Val - RHS.Val;
  669. else
  670. Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
  671. break;
  672. case tok::lessequal:
  673. Res = LHS.Val <= RHS.Val;
  674. Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
  675. break;
  676. case tok::less:
  677. Res = LHS.Val < RHS.Val;
  678. Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
  679. break;
  680. case tok::greaterequal:
  681. Res = LHS.Val >= RHS.Val;
  682. Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
  683. break;
  684. case tok::greater:
  685. Res = LHS.Val > RHS.Val;
  686. Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
  687. break;
  688. case tok::exclaimequal:
  689. Res = LHS.Val != RHS.Val;
  690. Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
  691. break;
  692. case tok::equalequal:
  693. Res = LHS.Val == RHS.Val;
  694. Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
  695. break;
  696. case tok::amp:
  697. Res = LHS.Val & RHS.Val;
  698. break;
  699. case tok::caret:
  700. Res = LHS.Val ^ RHS.Val;
  701. break;
  702. case tok::pipe:
  703. Res = LHS.Val | RHS.Val;
  704. break;
  705. case tok::ampamp:
  706. Res = (LHS.Val != 0 && RHS.Val != 0);
  707. Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
  708. break;
  709. case tok::pipepipe:
  710. Res = (LHS.Val != 0 || RHS.Val != 0);
  711. Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
  712. break;
  713. case tok::comma:
  714. // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
  715. // if not being evaluated.
  716. if (!PP.getLangOpts().C99 || ValueLive)
  717. PP.Diag(OpLoc, diag::ext_pp_comma_expr)
  718. << LHS.getRange() << RHS.getRange();
  719. Res = RHS.Val; // LHS = LHS,RHS -> RHS.
  720. break;
  721. case tok::question: {
  722. // Parse the : part of the expression.
  723. if (PeekTok.isNot(tok::colon)) {
  724. PP.Diag(PeekTok.getLocation(), diag::err_expected)
  725. << tok::colon << LHS.getRange() << RHS.getRange();
  726. PP.Diag(OpLoc, diag::note_matching) << tok::question;
  727. return true;
  728. }
  729. // Consume the :.
  730. PP.LexNonComment(PeekTok);
  731. // Evaluate the value after the :.
  732. bool AfterColonLive = ValueLive && LHS.Val == 0;
  733. PPValue AfterColonVal(LHS.getBitWidth());
  734. DefinedTracker DT;
  735. if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
  736. return true;
  737. // Parse anything after the : with the same precedence as ?. We allow
  738. // things of equal precedence because ?: is right associative.
  739. if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
  740. PeekTok, AfterColonLive,
  741. IncludedUndefinedIds, PP))
  742. return true;
  743. // Now that we have the condition, the LHS and the RHS of the :, evaluate.
  744. Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
  745. RHS.setEnd(AfterColonVal.getRange().getEnd());
  746. // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
  747. // either operand is unsigned.
  748. Res.setIsUnsigned(RHS.isUnsigned() || AfterColonVal.isUnsigned());
  749. // Figure out the precedence of the token after the : part.
  750. PeekPrec = getPrecedence(PeekTok.getKind());
  751. break;
  752. }
  753. case tok::colon:
  754. // Don't allow :'s to float around without being part of ?: exprs.
  755. PP.Diag(OpLoc, diag::err_pp_colon_without_question)
  756. << LHS.getRange() << RHS.getRange();
  757. return true;
  758. }
  759. // If this operator is live and overflowed, report the issue.
  760. if (Overflow && ValueLive)
  761. PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
  762. << LHS.getRange() << RHS.getRange();
  763. // Put the result back into 'LHS' for our next iteration.
  764. LHS.Val = Res;
  765. LHS.setEnd(RHS.getRange().getEnd());
  766. RHS.setIdentifier(nullptr);
  767. }
  768. }
  769. /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
  770. /// may occur after a #if or #elif directive. If the expression is equivalent
  771. /// to "!defined(X)" return X in IfNDefMacro.
  772. Preprocessor::DirectiveEvalResult
  773. Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
  774. SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
  775. // Save the current state of 'DisableMacroExpansion' and reset it to false. If
  776. // 'DisableMacroExpansion' is true, then we must be in a macro argument list
  777. // in which case a directive is undefined behavior. We want macros to be able
  778. // to recursively expand in order to get more gcc-list behavior, so we force
  779. // DisableMacroExpansion to false and restore it when we're done parsing the
  780. // expression.
  781. bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
  782. DisableMacroExpansion = false;
  783. // Peek ahead one token.
  784. Token Tok;
  785. LexNonComment(Tok);
  786. // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
  787. unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
  788. PPValue ResVal(BitWidth);
  789. DefinedTracker DT;
  790. SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
  791. if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
  792. // Parse error, skip the rest of the macro line.
  793. SourceRange ConditionRange = ExprStartLoc;
  794. if (Tok.isNot(tok::eod))
  795. ConditionRange = DiscardUntilEndOfDirective();
  796. // Restore 'DisableMacroExpansion'.
  797. DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
  798. // We cannot trust the source range from the value because there was a
  799. // parse error. Track the range manually -- the end of the directive is the
  800. // end of the condition range.
  801. return {false,
  802. DT.IncludedUndefinedIds,
  803. {ExprStartLoc, ConditionRange.getEnd()}};
  804. }
  805. // If we are at the end of the expression after just parsing a value, there
  806. // must be no (unparenthesized) binary operators involved, so we can exit
  807. // directly.
  808. if (Tok.is(tok::eod)) {
  809. // If the expression we parsed was of the form !defined(macro), return the
  810. // macro in IfNDefMacro.
  811. if (DT.State == DefinedTracker::NotDefinedMacro)
  812. IfNDefMacro = DT.TheMacro;
  813. // Restore 'DisableMacroExpansion'.
  814. DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
  815. return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
  816. }
  817. // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
  818. // operator and the stuff after it.
  819. if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
  820. Tok, true, DT.IncludedUndefinedIds, *this)) {
  821. // Parse error, skip the rest of the macro line.
  822. if (Tok.isNot(tok::eod))
  823. DiscardUntilEndOfDirective();
  824. // Restore 'DisableMacroExpansion'.
  825. DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
  826. return {false, DT.IncludedUndefinedIds, ResVal.getRange()};
  827. }
  828. // If we aren't at the tok::eod token, something bad happened, like an extra
  829. // ')' token.
  830. if (Tok.isNot(tok::eod)) {
  831. Diag(Tok, diag::err_pp_expected_eol);
  832. DiscardUntilEndOfDirective();
  833. }
  834. // Restore 'DisableMacroExpansion'.
  835. DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
  836. return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
  837. }