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