PPExpressions.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. // 'wb/uwb' literals are a C2x feature. We explicitly do not support the
  296. // suffix in C++ as an extension because a library-based UDL that resolves
  297. // to a library type may be more appropriate there.
  298. if (Literal.isBitInt)
  299. PP.Diag(PeekTok, PP.getLangOpts().C2x
  300. ? diag::warn_c2x_compat_bitint_suffix
  301. : diag::ext_c2x_bitint_suffix);
  302. // Parse the integer literal into Result.
  303. if (Literal.GetIntegerValue(Result.Val)) {
  304. // Overflow parsing integer literal.
  305. if (ValueLive)
  306. PP.Diag(PeekTok, diag::err_integer_literal_too_large)
  307. << /* Unsigned */ 1;
  308. Result.Val.setIsUnsigned(true);
  309. } else {
  310. // Set the signedness of the result to match whether there was a U suffix
  311. // or not.
  312. Result.Val.setIsUnsigned(Literal.isUnsigned);
  313. // Detect overflow based on whether the value is signed. If signed
  314. // and if the value is too large, emit a warning "integer constant is so
  315. // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
  316. // is 64-bits.
  317. if (!Literal.isUnsigned && Result.Val.isNegative()) {
  318. // Octal, hexadecimal, and binary literals are implicitly unsigned if
  319. // the value does not fit into a signed integer type.
  320. if (ValueLive && Literal.getRadix() == 10)
  321. PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
  322. Result.Val.setIsUnsigned(true);
  323. }
  324. }
  325. // Consume the token.
  326. Result.setRange(PeekTok.getLocation());
  327. PP.LexNonComment(PeekTok);
  328. return false;
  329. }
  330. case tok::char_constant: // 'x'
  331. case tok::wide_char_constant: // L'x'
  332. case tok::utf8_char_constant: // u8'x'
  333. case tok::utf16_char_constant: // u'x'
  334. case tok::utf32_char_constant: { // U'x'
  335. // Complain about, and drop, any ud-suffix.
  336. if (PeekTok.hasUDSuffix())
  337. PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
  338. SmallString<32> CharBuffer;
  339. bool CharInvalid = false;
  340. StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
  341. if (CharInvalid)
  342. return true;
  343. CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
  344. PeekTok.getLocation(), PP, PeekTok.getKind());
  345. if (Literal.hadError())
  346. return true; // A diagnostic was already emitted.
  347. // Character literals are always int or wchar_t, expand to intmax_t.
  348. const TargetInfo &TI = PP.getTargetInfo();
  349. unsigned NumBits;
  350. if (Literal.isMultiChar())
  351. NumBits = TI.getIntWidth();
  352. else if (Literal.isWide())
  353. NumBits = TI.getWCharWidth();
  354. else if (Literal.isUTF16())
  355. NumBits = TI.getChar16Width();
  356. else if (Literal.isUTF32())
  357. NumBits = TI.getChar32Width();
  358. else // char or char8_t
  359. NumBits = TI.getCharWidth();
  360. // Set the width.
  361. llvm::APSInt Val(NumBits);
  362. // Set the value.
  363. Val = Literal.getValue();
  364. // Set the signedness. UTF-16 and UTF-32 are always unsigned
  365. // UTF-8 is unsigned if -fchar8_t is specified.
  366. if (Literal.isWide())
  367. Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
  368. else if (Literal.isUTF16() || Literal.isUTF32())
  369. Val.setIsUnsigned(true);
  370. else if (Literal.isUTF8()) {
  371. if (PP.getLangOpts().CPlusPlus)
  372. Val.setIsUnsigned(
  373. PP.getLangOpts().Char8 ? true : !PP.getLangOpts().CharIsSigned);
  374. else
  375. Val.setIsUnsigned(true);
  376. } else
  377. Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
  378. if (Result.Val.getBitWidth() > Val.getBitWidth()) {
  379. Result.Val = Val.extend(Result.Val.getBitWidth());
  380. } else {
  381. assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
  382. "intmax_t smaller than char/wchar_t?");
  383. Result.Val = Val;
  384. }
  385. // Consume the token.
  386. Result.setRange(PeekTok.getLocation());
  387. PP.LexNonComment(PeekTok);
  388. return false;
  389. }
  390. case tok::l_paren: {
  391. SourceLocation Start = PeekTok.getLocation();
  392. PP.LexNonComment(PeekTok); // Eat the (.
  393. // Parse the value and if there are any binary operators involved, parse
  394. // them.
  395. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  396. // If this is a silly value like (X), which doesn't need parens, check for
  397. // !(defined X).
  398. if (PeekTok.is(tok::r_paren)) {
  399. // Just use DT unmodified as our result.
  400. } else {
  401. // Otherwise, we have something like (x+y), and we consumed '(x'.
  402. if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
  403. DT.IncludedUndefinedIds, PP))
  404. return true;
  405. if (PeekTok.isNot(tok::r_paren)) {
  406. PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
  407. << Result.getRange();
  408. PP.Diag(Start, diag::note_matching) << tok::l_paren;
  409. return true;
  410. }
  411. DT.State = DefinedTracker::Unknown;
  412. }
  413. Result.setRange(Start, PeekTok.getLocation());
  414. Result.setIdentifier(nullptr);
  415. PP.LexNonComment(PeekTok); // Eat the ).
  416. return false;
  417. }
  418. case tok::plus: {
  419. SourceLocation Start = PeekTok.getLocation();
  420. // Unary plus doesn't modify the value.
  421. PP.LexNonComment(PeekTok);
  422. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  423. Result.setBegin(Start);
  424. Result.setIdentifier(nullptr);
  425. return false;
  426. }
  427. case tok::minus: {
  428. SourceLocation Loc = PeekTok.getLocation();
  429. PP.LexNonComment(PeekTok);
  430. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  431. Result.setBegin(Loc);
  432. Result.setIdentifier(nullptr);
  433. // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
  434. Result.Val = -Result.Val;
  435. // -MININT is the only thing that overflows. Unsigned never overflows.
  436. bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
  437. // If this operator is live and overflowed, report the issue.
  438. if (Overflow && ValueLive)
  439. PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
  440. DT.State = DefinedTracker::Unknown;
  441. return false;
  442. }
  443. case tok::tilde: {
  444. SourceLocation Start = PeekTok.getLocation();
  445. PP.LexNonComment(PeekTok);
  446. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  447. Result.setBegin(Start);
  448. Result.setIdentifier(nullptr);
  449. // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
  450. Result.Val = ~Result.Val;
  451. DT.State = DefinedTracker::Unknown;
  452. return false;
  453. }
  454. case tok::exclaim: {
  455. SourceLocation Start = PeekTok.getLocation();
  456. PP.LexNonComment(PeekTok);
  457. if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
  458. Result.setBegin(Start);
  459. Result.Val = !Result.Val;
  460. // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
  461. Result.Val.setIsUnsigned(false);
  462. Result.setIdentifier(nullptr);
  463. if (DT.State == DefinedTracker::DefinedMacro)
  464. DT.State = DefinedTracker::NotDefinedMacro;
  465. else if (DT.State == DefinedTracker::NotDefinedMacro)
  466. DT.State = DefinedTracker::DefinedMacro;
  467. return false;
  468. }
  469. case tok::kw_true:
  470. case tok::kw_false:
  471. Result.Val = PeekTok.getKind() == tok::kw_true;
  472. Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
  473. Result.setIdentifier(PeekTok.getIdentifierInfo());
  474. Result.setRange(PeekTok.getLocation());
  475. PP.LexNonComment(PeekTok);
  476. return false;
  477. // FIXME: Handle #assert
  478. }
  479. }
  480. /// getPrecedence - Return the precedence of the specified binary operator
  481. /// token. This returns:
  482. /// ~0 - Invalid token.
  483. /// 14 -> 3 - various operators.
  484. /// 0 - 'eod' or ')'
  485. static unsigned getPrecedence(tok::TokenKind Kind) {
  486. switch (Kind) {
  487. default: return ~0U;
  488. case tok::percent:
  489. case tok::slash:
  490. case tok::star: return 14;
  491. case tok::plus:
  492. case tok::minus: return 13;
  493. case tok::lessless:
  494. case tok::greatergreater: return 12;
  495. case tok::lessequal:
  496. case tok::less:
  497. case tok::greaterequal:
  498. case tok::greater: return 11;
  499. case tok::exclaimequal:
  500. case tok::equalequal: return 10;
  501. case tok::amp: return 9;
  502. case tok::caret: return 8;
  503. case tok::pipe: return 7;
  504. case tok::ampamp: return 6;
  505. case tok::pipepipe: return 5;
  506. case tok::question: return 4;
  507. case tok::comma: return 3;
  508. case tok::colon: return 2;
  509. case tok::r_paren: return 0;// Lowest priority, end of expr.
  510. case tok::eod: return 0;// Lowest priority, end of directive.
  511. }
  512. }
  513. static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
  514. Token &Tok) {
  515. if (Tok.is(tok::l_paren) && LHS.getIdentifier())
  516. PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
  517. << LHS.getIdentifier();
  518. else
  519. PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
  520. << LHS.getRange();
  521. }
  522. /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
  523. /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
  524. ///
  525. /// If ValueLive is false, then this value is being evaluated in a context where
  526. /// the result is not used. As such, avoid diagnostics that relate to
  527. /// evaluation, such as division by zero warnings.
  528. static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
  529. Token &PeekTok, bool ValueLive,
  530. bool &IncludedUndefinedIds,
  531. Preprocessor &PP) {
  532. unsigned PeekPrec = getPrecedence(PeekTok.getKind());
  533. // If this token isn't valid, report the error.
  534. if (PeekPrec == ~0U) {
  535. diagnoseUnexpectedOperator(PP, LHS, PeekTok);
  536. return true;
  537. }
  538. while (true) {
  539. // If this token has a lower precedence than we are allowed to parse, return
  540. // it so that higher levels of the recursion can parse it.
  541. if (PeekPrec < MinPrec)
  542. return false;
  543. tok::TokenKind Operator = PeekTok.getKind();
  544. // If this is a short-circuiting operator, see if the RHS of the operator is
  545. // dead. Note that this cannot just clobber ValueLive. Consider
  546. // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
  547. // this example, the RHS of the && being dead does not make the rest of the
  548. // expr dead.
  549. bool RHSIsLive;
  550. if (Operator == tok::ampamp && LHS.Val == 0)
  551. RHSIsLive = false; // RHS of "0 && x" is dead.
  552. else if (Operator == tok::pipepipe && LHS.Val != 0)
  553. RHSIsLive = false; // RHS of "1 || x" is dead.
  554. else if (Operator == tok::question && LHS.Val == 0)
  555. RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
  556. else
  557. RHSIsLive = ValueLive;
  558. // Consume the operator, remembering the operator's location for reporting.
  559. SourceLocation OpLoc = PeekTok.getLocation();
  560. PP.LexNonComment(PeekTok);
  561. PPValue RHS(LHS.getBitWidth());
  562. // Parse the RHS of the operator.
  563. DefinedTracker DT;
  564. if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
  565. IncludedUndefinedIds = DT.IncludedUndefinedIds;
  566. // Remember the precedence of this operator and get the precedence of the
  567. // operator immediately to the right of the RHS.
  568. unsigned ThisPrec = PeekPrec;
  569. PeekPrec = getPrecedence(PeekTok.getKind());
  570. // If this token isn't valid, report the error.
  571. if (PeekPrec == ~0U) {
  572. diagnoseUnexpectedOperator(PP, RHS, PeekTok);
  573. return true;
  574. }
  575. // Decide whether to include the next binop in this subexpression. For
  576. // example, when parsing x+y*z and looking at '*', we want to recursively
  577. // handle y*z as a single subexpression. We do this because the precedence
  578. // of * is higher than that of +. The only strange case we have to handle
  579. // here is for the ?: operator, where the precedence is actually lower than
  580. // the LHS of the '?'. The grammar rule is:
  581. //
  582. // conditional-expression ::=
  583. // logical-OR-expression ? expression : conditional-expression
  584. // where 'expression' is actually comma-expression.
  585. unsigned RHSPrec;
  586. if (Operator == tok::question)
  587. // The RHS of "?" should be maximally consumed as an expression.
  588. RHSPrec = getPrecedence(tok::comma);
  589. else // All others should munch while higher precedence.
  590. RHSPrec = ThisPrec+1;
  591. if (PeekPrec >= RHSPrec) {
  592. if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
  593. IncludedUndefinedIds, PP))
  594. return true;
  595. PeekPrec = getPrecedence(PeekTok.getKind());
  596. }
  597. assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
  598. // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
  599. // either operand is unsigned.
  600. llvm::APSInt Res(LHS.getBitWidth());
  601. switch (Operator) {
  602. case tok::question: // No UAC for x and y in "x ? y : z".
  603. case tok::lessless: // Shift amount doesn't UAC with shift value.
  604. case tok::greatergreater: // Shift amount doesn't UAC with shift value.
  605. case tok::comma: // Comma operands are not subject to UACs.
  606. case tok::pipepipe: // Logical || does not do UACs.
  607. case tok::ampamp: // Logical && does not do UACs.
  608. break; // No UAC
  609. default:
  610. Res.setIsUnsigned(LHS.isUnsigned() || RHS.isUnsigned());
  611. // If this just promoted something from signed to unsigned, and if the
  612. // value was negative, warn about it.
  613. if (ValueLive && Res.isUnsigned()) {
  614. if (!LHS.isUnsigned() && LHS.Val.isNegative())
  615. PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
  616. << toString(LHS.Val, 10, true) + " to " +
  617. toString(LHS.Val, 10, false)
  618. << LHS.getRange() << RHS.getRange();
  619. if (!RHS.isUnsigned() && RHS.Val.isNegative())
  620. PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
  621. << toString(RHS.Val, 10, true) + " to " +
  622. toString(RHS.Val, 10, false)
  623. << LHS.getRange() << RHS.getRange();
  624. }
  625. LHS.Val.setIsUnsigned(Res.isUnsigned());
  626. RHS.Val.setIsUnsigned(Res.isUnsigned());
  627. }
  628. bool Overflow = false;
  629. switch (Operator) {
  630. default: llvm_unreachable("Unknown operator token!");
  631. case tok::percent:
  632. if (RHS.Val != 0)
  633. Res = LHS.Val % RHS.Val;
  634. else if (ValueLive) {
  635. PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
  636. << LHS.getRange() << RHS.getRange();
  637. return true;
  638. }
  639. break;
  640. case tok::slash:
  641. if (RHS.Val != 0) {
  642. if (LHS.Val.isSigned())
  643. Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
  644. else
  645. Res = LHS.Val / RHS.Val;
  646. } else if (ValueLive) {
  647. PP.Diag(OpLoc, diag::err_pp_division_by_zero)
  648. << LHS.getRange() << RHS.getRange();
  649. return true;
  650. }
  651. break;
  652. case tok::star:
  653. if (Res.isSigned())
  654. Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
  655. else
  656. Res = LHS.Val * RHS.Val;
  657. break;
  658. case tok::lessless: {
  659. // Determine whether overflow is about to happen.
  660. if (LHS.isUnsigned())
  661. Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
  662. else
  663. Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
  664. break;
  665. }
  666. case tok::greatergreater: {
  667. // Determine whether overflow is about to happen.
  668. unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
  669. if (ShAmt >= LHS.getBitWidth()) {
  670. Overflow = true;
  671. ShAmt = LHS.getBitWidth()-1;
  672. }
  673. Res = LHS.Val >> ShAmt;
  674. break;
  675. }
  676. case tok::plus:
  677. if (LHS.isUnsigned())
  678. Res = LHS.Val + RHS.Val;
  679. else
  680. Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
  681. break;
  682. case tok::minus:
  683. if (LHS.isUnsigned())
  684. Res = LHS.Val - RHS.Val;
  685. else
  686. Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
  687. break;
  688. case tok::lessequal:
  689. Res = LHS.Val <= RHS.Val;
  690. Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
  691. break;
  692. case tok::less:
  693. Res = LHS.Val < RHS.Val;
  694. Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
  695. break;
  696. case tok::greaterequal:
  697. Res = LHS.Val >= RHS.Val;
  698. Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
  699. break;
  700. case tok::greater:
  701. Res = LHS.Val > RHS.Val;
  702. Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
  703. break;
  704. case tok::exclaimequal:
  705. Res = LHS.Val != RHS.Val;
  706. Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
  707. break;
  708. case tok::equalequal:
  709. Res = LHS.Val == RHS.Val;
  710. Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
  711. break;
  712. case tok::amp:
  713. Res = LHS.Val & RHS.Val;
  714. break;
  715. case tok::caret:
  716. Res = LHS.Val ^ RHS.Val;
  717. break;
  718. case tok::pipe:
  719. Res = LHS.Val | RHS.Val;
  720. break;
  721. case tok::ampamp:
  722. Res = (LHS.Val != 0 && RHS.Val != 0);
  723. Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
  724. break;
  725. case tok::pipepipe:
  726. Res = (LHS.Val != 0 || RHS.Val != 0);
  727. Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
  728. break;
  729. case tok::comma:
  730. // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
  731. // if not being evaluated.
  732. if (!PP.getLangOpts().C99 || ValueLive)
  733. PP.Diag(OpLoc, diag::ext_pp_comma_expr)
  734. << LHS.getRange() << RHS.getRange();
  735. Res = RHS.Val; // LHS = LHS,RHS -> RHS.
  736. break;
  737. case tok::question: {
  738. // Parse the : part of the expression.
  739. if (PeekTok.isNot(tok::colon)) {
  740. PP.Diag(PeekTok.getLocation(), diag::err_expected)
  741. << tok::colon << LHS.getRange() << RHS.getRange();
  742. PP.Diag(OpLoc, diag::note_matching) << tok::question;
  743. return true;
  744. }
  745. // Consume the :.
  746. PP.LexNonComment(PeekTok);
  747. // Evaluate the value after the :.
  748. bool AfterColonLive = ValueLive && LHS.Val == 0;
  749. PPValue AfterColonVal(LHS.getBitWidth());
  750. DefinedTracker DT;
  751. if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
  752. return true;
  753. // Parse anything after the : with the same precedence as ?. We allow
  754. // things of equal precedence because ?: is right associative.
  755. if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
  756. PeekTok, AfterColonLive,
  757. IncludedUndefinedIds, PP))
  758. return true;
  759. // Now that we have the condition, the LHS and the RHS of the :, evaluate.
  760. Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
  761. RHS.setEnd(AfterColonVal.getRange().getEnd());
  762. // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
  763. // either operand is unsigned.
  764. Res.setIsUnsigned(RHS.isUnsigned() || AfterColonVal.isUnsigned());
  765. // Figure out the precedence of the token after the : part.
  766. PeekPrec = getPrecedence(PeekTok.getKind());
  767. break;
  768. }
  769. case tok::colon:
  770. // Don't allow :'s to float around without being part of ?: exprs.
  771. PP.Diag(OpLoc, diag::err_pp_colon_without_question)
  772. << LHS.getRange() << RHS.getRange();
  773. return true;
  774. }
  775. // If this operator is live and overflowed, report the issue.
  776. if (Overflow && ValueLive)
  777. PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
  778. << LHS.getRange() << RHS.getRange();
  779. // Put the result back into 'LHS' for our next iteration.
  780. LHS.Val = Res;
  781. LHS.setEnd(RHS.getRange().getEnd());
  782. RHS.setIdentifier(nullptr);
  783. }
  784. }
  785. /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
  786. /// may occur after a #if or #elif directive. If the expression is equivalent
  787. /// to "!defined(X)" return X in IfNDefMacro.
  788. Preprocessor::DirectiveEvalResult
  789. Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
  790. SaveAndRestore PPDir(ParsingIfOrElifDirective, true);
  791. // Save the current state of 'DisableMacroExpansion' and reset it to false. If
  792. // 'DisableMacroExpansion' is true, then we must be in a macro argument list
  793. // in which case a directive is undefined behavior. We want macros to be able
  794. // to recursively expand in order to get more gcc-list behavior, so we force
  795. // DisableMacroExpansion to false and restore it when we're done parsing the
  796. // expression.
  797. bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
  798. DisableMacroExpansion = false;
  799. // Peek ahead one token.
  800. Token Tok;
  801. LexNonComment(Tok);
  802. // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
  803. unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
  804. PPValue ResVal(BitWidth);
  805. DefinedTracker DT;
  806. SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
  807. if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
  808. // Parse error, skip the rest of the macro line.
  809. SourceRange ConditionRange = ExprStartLoc;
  810. if (Tok.isNot(tok::eod))
  811. ConditionRange = DiscardUntilEndOfDirective();
  812. // Restore 'DisableMacroExpansion'.
  813. DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
  814. // We cannot trust the source range from the value because there was a
  815. // parse error. Track the range manually -- the end of the directive is the
  816. // end of the condition range.
  817. return {false,
  818. DT.IncludedUndefinedIds,
  819. {ExprStartLoc, ConditionRange.getEnd()}};
  820. }
  821. // If we are at the end of the expression after just parsing a value, there
  822. // must be no (unparenthesized) binary operators involved, so we can exit
  823. // directly.
  824. if (Tok.is(tok::eod)) {
  825. // If the expression we parsed was of the form !defined(macro), return the
  826. // macro in IfNDefMacro.
  827. if (DT.State == DefinedTracker::NotDefinedMacro)
  828. IfNDefMacro = DT.TheMacro;
  829. // Restore 'DisableMacroExpansion'.
  830. DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
  831. return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
  832. }
  833. // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
  834. // operator and the stuff after it.
  835. if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
  836. Tok, true, DT.IncludedUndefinedIds, *this)) {
  837. // Parse error, skip the rest of the macro line.
  838. if (Tok.isNot(tok::eod))
  839. DiscardUntilEndOfDirective();
  840. // Restore 'DisableMacroExpansion'.
  841. DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
  842. return {false, DT.IncludedUndefinedIds, ResVal.getRange()};
  843. }
  844. // If we aren't at the tok::eod token, something bad happened, like an extra
  845. // ')' token.
  846. if (Tok.isNot(tok::eod)) {
  847. Diag(Tok, diag::err_pp_expected_eol);
  848. DiscardUntilEndOfDirective();
  849. }
  850. // Restore 'DisableMacroExpansion'.
  851. DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
  852. return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
  853. }