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