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