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