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