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