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