PPExpressions.cpp 33 KB

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