blob: 66a12eca0ffdf27c0af833bebee52ddb33320083 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner80965422006-07-04 18:03:19 +000010// This file implements the Preprocessor::EvaluateDirectiveExpression method,
11// which parses and evaluates integer constant expressions for #if directives.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14//
Chris Lattner6df79752007-04-04 06:54:19 +000015// FIXME: implement testing for #assert's.
Chris Lattner22eb9722006-06-18 05:43:12 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "clang/Lex/Preprocessor.h"
Chris Lattnera78a97e2006-07-03 05:42:18 +000020#include "clang/Lex/MacroInfo.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000021#include "clang/Lex/LiteralSupport.h"
Douglas Gregor12785102010-08-24 20:21:13 +000022#include "clang/Lex/CodeCompletionHandler.h"
Chris Lattner81278c62006-10-14 19:03:49 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner60f36222009-01-29 05:15:15 +000024#include "clang/Lex/LexDiagnostic.h"
Chris Lattnera9eac7f2007-04-05 05:24:00 +000025#include "llvm/ADT/APSInt.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000027using namespace clang;
28
Dan Gohman28ade552010-07-26 21:25:24 +000029namespace {
30
Chris Lattner3565c8e2008-05-05 06:45:50 +000031/// PPValue - Represents the value of a subexpression of a preprocessor
32/// conditional and the source range covered by it.
33class PPValue {
34 SourceRange Range;
35public:
36 llvm::APSInt Val;
Mike Stump11289f42009-09-09 15:08:12 +000037
Chris Lattner3565c8e2008-05-05 06:45:50 +000038 // Default ctor - Construct an 'invalid' PPValue.
39 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump11289f42009-09-09 15:08:12 +000040
Chris Lattner3565c8e2008-05-05 06:45:50 +000041 unsigned getBitWidth() const { return Val.getBitWidth(); }
42 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump11289f42009-09-09 15:08:12 +000043
Chris Lattner3565c8e2008-05-05 06:45:50 +000044 const SourceRange &getRange() const { return Range; }
Mike Stump11289f42009-09-09 15:08:12 +000045
Chris Lattner3565c8e2008-05-05 06:45:50 +000046 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
47 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump11289f42009-09-09 15:08:12 +000048 Range.setBegin(B); Range.setEnd(E);
Chris Lattner3565c8e2008-05-05 06:45:50 +000049 }
50 void setBegin(SourceLocation L) { Range.setBegin(L); }
51 void setEnd(SourceLocation L) { Range.setEnd(L); }
52};
53
Dan Gohman28ade552010-07-26 21:25:24 +000054}
55
Chris Lattner3565c8e2008-05-05 06:45:50 +000056static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +000057 Token &PeekTok, bool ValueLive,
Chris Lattner86054a92007-04-10 05:26:38 +000058 Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000059
Chris Lattnerb9d90f72006-07-04 18:32:03 +000060/// DefinedTracker - This struct is used while parsing expressions to keep track
61/// of whether !defined(X) has been seen.
62///
63/// With this simple scheme, we handle the basic forms:
64/// !defined(X) and !defined X
65/// but we also trivially handle (silly) stuff like:
66/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
67struct DefinedTracker {
68 /// Each time a Value is evaluated, it returns information about whether the
69 /// parsed value is of the form defined(X), !defined(X) or is something else.
70 enum TrackerState {
71 DefinedMacro, // defined(X)
72 NotDefinedMacro, // !defined(X)
73 Unknown // Something else.
74 } State;
75 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
76 /// indicates the macro that was checked.
77 IdentifierInfo *TheMacro;
78};
79
John Thompsonb5353522009-10-30 13:49:06 +000080/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattner4c53c402009-12-14 05:00:18 +000081static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
82 bool ValueLive, Preprocessor &PP) {
John Thompsonb5353522009-10-30 13:49:06 +000083 IdentifierInfo *II;
84 Result.setBegin(PeekTok.getLocation());
85
86 // Get the next token, don't expand it.
Eli Friedmanb3bfd842011-08-03 00:04:13 +000087 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +000088
89 // Two options, it can either be a pp-identifier or a (.
90 SourceLocation LParenLoc;
91 if (PeekTok.is(tok::l_paren)) {
92 // Found a paren, remember we saw it and skip it.
93 LParenLoc = PeekTok.getLocation();
Eli Friedmanb3bfd842011-08-03 00:04:13 +000094 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +000095 }
96
Douglas Gregor12785102010-08-24 20:21:13 +000097 if (PeekTok.is(tok::code_completion)) {
98 if (PP.getCodeCompletionHandler())
99 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000100 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000101 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000102 }
103
John Thompsonb5353522009-10-30 13:49:06 +0000104 // If we don't have a pp-identifier now, this is an error.
105 if ((II = PeekTok.getIdentifierInfo()) == 0) {
106 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
107 return true;
108 }
109
110 // Otherwise, we got an identifier, is it defined to something?
111 Result.Val = II->hasMacroDefinition();
112 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
113
114 // If there is a macro, mark it used.
115 if (Result.Val != 0 && ValueLive) {
116 MacroInfo *Macro = PP.getMacroInfo(II);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000117 PP.markMacroAsUsed(Macro);
John Thompsonb5353522009-10-30 13:49:06 +0000118 }
119
John Thompsonb5353522009-10-30 13:49:06 +0000120 // If we are in parens, ensure we have a trailing ).
121 if (LParenLoc.isValid()) {
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000122 // Consume identifier.
123 Result.setEnd(PeekTok.getLocation());
124 PP.LexUnexpandedNonComment(PeekTok);
125
John Thompsonb5353522009-10-30 13:49:06 +0000126 if (PeekTok.isNot(tok::r_paren)) {
127 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined";
128 PP.Diag(LParenLoc, diag::note_matching) << "(";
129 return true;
130 }
131 // Consume the ).
132 Result.setEnd(PeekTok.getLocation());
133 PP.LexNonComment(PeekTok);
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000134 } else {
135 // Consume identifier.
136 Result.setEnd(PeekTok.getLocation());
137 PP.LexNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000138 }
139
140 // Success, remember that we saw defined(X).
141 DT.State = DefinedTracker::DefinedMacro;
142 DT.TheMacro = II;
143 return false;
144}
145
Chris Lattner22eb9722006-06-18 05:43:12 +0000146/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
147/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000148/// parsing. This function also returns information about the form of the
149/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +0000150///
151/// If ValueLive is false, then this value is being evaluated in a context where
152/// the result is not used. As such, avoid diagnostics that relate to
153/// evaluation.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000154static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
155 bool ValueLive, Preprocessor &PP) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000156 DT.State = DefinedTracker::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000157
Douglas Gregorec00a262010-08-24 22:20:20 +0000158 if (PeekTok.is(tok::code_completion)) {
159 if (PP.getCodeCompletionHandler())
160 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000161 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000162 PP.LexNonComment(PeekTok);
Douglas Gregorec00a262010-08-24 22:20:20 +0000163 }
164
Chris Lattner22eb9722006-06-18 05:43:12 +0000165 // If this token's spelling is a pp-identifier, check to see if it is
166 // 'defined' or if it is a macro. Note that we check here because many
167 // keywords are pp-identifiers, so we can't check the kind.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000168 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner676268e2009-12-14 04:26:45 +0000169 // Handle "defined X" and "defined(X)".
170 if (II->isStr("defined"))
John Thompsonb5353522009-10-30 13:49:06 +0000171 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattner676268e2009-12-14 04:26:45 +0000172
173 // If this identifier isn't 'defined' or one of the special
174 // preprocessor keywords and it wasn't macro expanded, it turns
175 // into a simple 0, unless it is the C++ keyword "true", in which case it
176 // turns into "1".
177 if (ValueLive)
178 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
179 Result.Val = II->getTokenID() == tok::kw_true;
180 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
181 Result.setRange(PeekTok.getLocation());
182 PP.LexNonComment(PeekTok);
183 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000184 }
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner22eb9722006-06-18 05:43:12 +0000186 switch (PeekTok.getKind()) {
187 default: // Non-value token.
Chris Lattnerf8f94542008-04-13 20:38:43 +0000188 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000189 return true;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000190 case tok::eod:
Chris Lattner22eb9722006-06-18 05:43:12 +0000191 case tok::r_paren:
192 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000193 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000194 return true;
195 case tok::numeric_constant: {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000196 llvm::SmallString<64> IntegerBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000197 bool NumberInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000198 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000199 &NumberInvalid);
200 if (NumberInvalid)
201 return true; // a diagnostic was already reported
202
Benjamin Kramer53fa3472010-02-27 16:29:36 +0000203 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Steve Naroff451d8f162007-03-12 23:22:38 +0000204 PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000205 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000206 return true; // a diagnostic was already reported.
Mike Stump11289f42009-09-09 15:08:12 +0000207
Chris Lattnered045422007-08-26 03:29:23 +0000208 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000209 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000210 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000211 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000212 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000213
Neil Boothac582c52007-08-29 22:00:19 +0000214 // long long is a C99 feature.
215 if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
Neil Booth4a1ee052007-08-29 22:13:52 +0000216 && Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000217 PP.Diag(PeekTok, diag::ext_longlong);
218
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000219 // Parse the integer literal into Result.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000220 if (Literal.GetIntegerValue(Result.Val)) {
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000221 // Overflow parsing integer literal.
Chris Lattner86054a92007-04-10 05:26:38 +0000222 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000223 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000224 } else {
225 // Set the signedness of the result to match whether there was a U suffix
226 // or not.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000227 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump11289f42009-09-09 15:08:12 +0000228
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000229 // Detect overflow based on whether the value is signed. If signed
230 // and if the value is too large, emit a warning "integer constant is so
231 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
232 // is 64-bits.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000233 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattner1cb0e612008-07-03 03:47:30 +0000234 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
235 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner3565c8e2008-05-05 06:45:50 +0000236 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
237 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000238 }
239 }
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000241 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000242 Result.setRange(PeekTok.getLocation());
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000243 PP.LexNonComment(PeekTok);
244 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000245 }
Douglas Gregorfb65e592011-07-27 05:40:30 +0000246 case tok::char_constant: // 'x'
247 case tok::wide_char_constant: { // L'x'
248 case tok::utf16_char_constant: // u'x'
249 case tok::utf32_char_constant: // U'x'
Chris Lattner23b7eb62007-06-15 23:05:46 +0000250 llvm::SmallString<32> CharBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000251 bool CharInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000252 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000253 if (CharInvalid)
254 return true;
Benjamin Kramer0a1abd42010-02-27 13:44:12 +0000255
256 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregorfb65e592011-07-27 05:40:30 +0000257 PeekTok.getLocation(), PP, PeekTok.getKind());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000258 if (Literal.hadError())
259 return true; // A diagnostic was already emitted.
260
261 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar1b444192009-11-13 05:51:54 +0000262 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedmand8cec572009-06-01 05:25:02 +0000263 unsigned NumBits;
264 if (Literal.isMultiChar())
265 NumBits = TI.getIntWidth();
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000266 else if (Literal.isWide())
267 NumBits = TI.getWCharWidth();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000268 else if (Literal.isUTF16())
269 NumBits = TI.getChar16Width();
270 else if (Literal.isUTF32())
271 NumBits = TI.getChar32Width();
Eli Friedmand8cec572009-06-01 05:25:02 +0000272 else
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000273 NumBits = TI.getCharWidth();
Eli Friedmand8cec572009-06-01 05:25:02 +0000274
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000275 // Set the width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000276 llvm::APSInt Val(NumBits);
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000277 // Set the value.
278 Val = Literal.getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000279 // Set the signedness. UTF-16 and UTF-32 are always unsigned
280 if (!Literal.isUTF16() && !Literal.isUTF32())
281 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner3565c8e2008-05-05 06:45:50 +0000283 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
284 Result.Val = Val.extend(Result.Val.getBitWidth());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000285 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000286 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000287 "intmax_t smaller than char/wchar_t?");
Chris Lattner3565c8e2008-05-05 06:45:50 +0000288 Result.Val = Val;
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000289 }
290
291 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000292 Result.setRange(PeekTok.getLocation());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000293 PP.LexNonComment(PeekTok);
294 return false;
295 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000296 case tok::l_paren: {
297 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000298 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000299 // Parse the value and if there are any binary operators involved, parse
300 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000301 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000302
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000303 // If this is a silly value like (X), which doesn't need parens, check for
304 // !(defined X).
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000305 if (PeekTok.is(tok::r_paren)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000306 // Just use DT unmodified as our result.
307 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000308 // Otherwise, we have something like (x+y), and we consumed '(x'.
Chris Lattner86054a92007-04-10 05:26:38 +0000309 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
310 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000311
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000312 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000313 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
314 << Result.getRange();
Chris Lattner03c40412008-11-23 23:17:07 +0000315 PP.Diag(Start, diag::note_matching) << "(";
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000316 return true;
317 }
318 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000319 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000320 Result.setRange(Start, PeekTok.getLocation());
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000321 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000322 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000323 }
324 case tok::plus: {
325 SourceLocation Start = PeekTok.getLocation();
Chris Lattner22eb9722006-06-18 05:43:12 +0000326 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000327 PP.LexNonComment(PeekTok);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000328 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
329 Result.setBegin(Start);
330 return false;
331 }
Chris Lattner7e61ac52007-04-11 03:42:36 +0000332 case tok::minus: {
333 SourceLocation Loc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000334 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000335 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000336 Result.setBegin(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000337
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000338 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000339 Result.Val = -Result.Val;
Mike Stump11289f42009-09-09 15:08:12 +0000340
Chris Lattner1cb0e612008-07-03 03:47:30 +0000341 // -MININT is the only thing that overflows. Unsigned never overflows.
342 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattner7e61ac52007-04-11 03:42:36 +0000344 // If this operator is live and overflowed, report the issue.
345 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000346 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000347
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000348 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000349 return false;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattner3565c8e2008-05-05 06:45:50 +0000352 case tok::tilde: {
353 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000354 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000355 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000356 Result.setBegin(Start);
357
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000358 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000359 Result.Val = ~Result.Val;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000360 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000361 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000362 }
Mike Stump11289f42009-09-09 15:08:12 +0000363
Chris Lattner3565c8e2008-05-05 06:45:50 +0000364 case tok::exclaim: {
365 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000366 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000367 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000368 Result.setBegin(Start);
369 Result.Val = !Result.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000370 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000371 Result.Val.setIsUnsigned(false);
Mike Stump11289f42009-09-09 15:08:12 +0000372
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000373 if (DT.State == DefinedTracker::DefinedMacro)
374 DT.State = DefinedTracker::NotDefinedMacro;
375 else if (DT.State == DefinedTracker::NotDefinedMacro)
376 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000377 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000378 }
Mike Stump11289f42009-09-09 15:08:12 +0000379
Chris Lattner22eb9722006-06-18 05:43:12 +0000380 // FIXME: Handle #assert
381 }
382}
383
384
385
386/// getPrecedence - Return the precedence of the specified binary operator
387/// token. This returns:
388/// ~0 - Invalid token.
Chris Lattner3c57f7e2008-05-05 04:10:51 +0000389/// 14 -> 3 - various operators.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000390/// 0 - 'eod' or ')'
Chris Lattner22eb9722006-06-18 05:43:12 +0000391static unsigned getPrecedence(tok::TokenKind Kind) {
392 switch (Kind) {
393 default: return ~0U;
394 case tok::percent:
395 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000396 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000397 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000398 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000399 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000400 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000401 case tok::lessequal:
402 case tok::less:
403 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000404 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000405 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000406 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000407 case tok::amp: return 9;
408 case tok::caret: return 8;
409 case tok::pipe: return 7;
410 case tok::ampamp: return 6;
411 case tok::pipepipe: return 5;
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000412 case tok::question: return 4;
413 case tok::comma: return 3;
Chris Lattnerd89e4582008-05-04 18:36:18 +0000414 case tok::colon: return 2;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000415 case tok::r_paren: return 0;// Lowest priority, end of expr.
416 case tok::eod: return 0;// Lowest priority, end of directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000417 }
418}
419
420
421/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner3565c8e2008-05-05 06:45:50 +0000422/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Chris Lattner86054a92007-04-10 05:26:38 +0000423///
424/// If ValueLive is false, then this value is being evaluated in a context where
425/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner3565c8e2008-05-05 06:45:50 +0000426/// evaluation, such as division by zero warnings.
427static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +0000428 Token &PeekTok, bool ValueLive,
Chris Lattner86054a92007-04-10 05:26:38 +0000429 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000430 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
431 // If this token isn't valid, report the error.
432 if (PeekPrec == ~0U) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000433 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
434 << LHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000435 return true;
436 }
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattner22eb9722006-06-18 05:43:12 +0000438 while (1) {
439 // If this token has a lower precedence than we are allowed to parse, return
440 // it so that higher levels of the recursion can parse it.
441 if (PeekPrec < MinPrec)
442 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattner22eb9722006-06-18 05:43:12 +0000444 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattner86054a92007-04-10 05:26:38 +0000446 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump11289f42009-09-09 15:08:12 +0000447 // dead. Note that this cannot just clobber ValueLive. Consider
Chris Lattner86054a92007-04-10 05:26:38 +0000448 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
449 // this example, the RHS of the && being dead does not make the rest of the
450 // expr dead.
451 bool RHSIsLive;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000452 if (Operator == tok::ampamp && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000453 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000454 else if (Operator == tok::pipepipe && LHS.Val != 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000455 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000456 else if (Operator == tok::question && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000457 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
458 else
459 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000460
Chris Lattner3565c8e2008-05-05 06:45:50 +0000461 // Consume the operator, remembering the operator's location for reporting.
462 SourceLocation OpLoc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000463 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000464
Chris Lattner3565c8e2008-05-05 06:45:50 +0000465 PPValue RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000466 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000467 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000468 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000469
470 // Remember the precedence of this operator and get the precedence of the
471 // operator immediately to the right of the RHS.
472 unsigned ThisPrec = PeekPrec;
473 PeekPrec = getPrecedence(PeekTok.getKind());
474
475 // If this token isn't valid, report the error.
476 if (PeekPrec == ~0U) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000477 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
478 << RHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000479 return true;
480 }
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000482 // Decide whether to include the next binop in this subexpression. For
483 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattnerca2b3182008-05-05 20:09:27 +0000484 // handle y*z as a single subexpression. We do this because the precedence
485 // of * is higher than that of +. The only strange case we have to handle
486 // here is for the ?: operator, where the precedence is actually lower than
487 // the LHS of the '?'. The grammar rule is:
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000488 //
489 // conditional-expression ::=
490 // logical-OR-expression ? expression : conditional-expression
491 // where 'expression' is actually comma-expression.
492 unsigned RHSPrec;
493 if (Operator == tok::question)
494 // The RHS of "?" should be maximally consumed as an expression.
495 RHSPrec = getPrecedence(tok::comma);
496 else // All others should munch while higher precedence.
497 RHSPrec = ThisPrec+1;
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000499 if (PeekPrec >= RHSPrec) {
500 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000501 return true;
502 PeekPrec = getPrecedence(PeekTok.getKind());
503 }
504 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump11289f42009-09-09 15:08:12 +0000505
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000506 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump11289f42009-09-09 15:08:12 +0000507 // either operand is unsigned.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000508 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattnerca671b02008-05-04 23:46:17 +0000509 switch (Operator) {
510 case tok::question: // No UAC for x and y in "x ? y : z".
511 case tok::lessless: // Shift amount doesn't UAC with shift value.
512 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
513 case tok::comma: // Comma operands are not subject to UACs.
514 case tok::pipepipe: // Logical || does not do UACs.
515 case tok::ampamp: // Logical && does not do UACs.
516 break; // No UAC
517 default:
Chris Lattner99ca0912007-04-11 04:14:45 +0000518 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
519 // If this just promoted something from signed to unsigned, and if the
520 // value was negative, warn about it.
521 if (ValueLive && Res.isUnsigned()) {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000522 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattnere05c4df2008-11-18 21:48:13 +0000523 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
524 << LHS.Val.toString(10, true) + " to " +
525 LHS.Val.toString(10, false)
526 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000527 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattnere05c4df2008-11-18 21:48:13 +0000528 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
529 << RHS.Val.toString(10, true) + " to " +
530 RHS.Val.toString(10, false)
531 << LHS.getRange() << RHS.getRange();
Chris Lattner99ca0912007-04-11 04:14:45 +0000532 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000533 LHS.Val.setIsUnsigned(Res.isUnsigned());
534 RHS.Val.setIsUnsigned(Res.isUnsigned());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000535 }
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattner5a0f1642007-04-10 06:54:33 +0000537 bool Overflow = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000538 switch (Operator) {
David Blaikie83d382b2011-09-23 05:06:16 +0000539 default: llvm_unreachable("Unknown operator token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000540 case tok::percent:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000541 if (RHS.Val != 0)
542 Res = LHS.Val % RHS.Val;
543 else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000544 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
545 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000546 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000547 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000548 break;
549 case tok::slash:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000550 if (RHS.Val != 0) {
Chris Lattner2edb9262010-10-13 23:46:56 +0000551 if (LHS.Val.isSigned())
552 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
553 else
554 Res = LHS.Val / RHS.Val;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000555 } else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000556 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
557 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000558 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000559 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000560 break;
Mike Stump11289f42009-09-09 15:08:12 +0000561
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000562 case tok::star:
Chris Lattner2edb9262010-10-13 23:46:56 +0000563 if (Res.isSigned())
564 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
565 else
566 Res = LHS.Val * RHS.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000567 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000568 case tok::lessless: {
569 // Determine whether overflow is about to happen.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000570 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattner2edb9262010-10-13 23:46:56 +0000571 if (LHS.isUnsigned()) {
572 Overflow = ShAmt >= LHS.Val.getBitWidth();
573 if (Overflow)
574 ShAmt = LHS.Val.getBitWidth()-1;
575 Res = LHS.Val << ShAmt;
576 } else {
577 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
578 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000579 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000580 }
581 case tok::greatergreater: {
582 // Determine whether overflow is about to happen.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000583 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattner5a0f1642007-04-10 06:54:33 +0000584 if (ShAmt >= LHS.getBitWidth())
585 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000586 Res = LHS.Val >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000587 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000588 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000589 case tok::plus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000590 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000591 Res = LHS.Val + RHS.Val;
592 else
593 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000594 break;
595 case tok::minus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000596 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000597 Res = LHS.Val - RHS.Val;
598 else
599 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000600 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000601 case tok::lessequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000602 Res = LHS.Val <= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000603 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000604 break;
605 case tok::less:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000606 Res = LHS.Val < RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000607 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000608 break;
609 case tok::greaterequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000610 Res = LHS.Val >= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000611 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000612 break;
613 case tok::greater:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000614 Res = LHS.Val > RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000615 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000616 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000617 case tok::exclaimequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000618 Res = LHS.Val != RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000619 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000620 break;
621 case tok::equalequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000622 Res = LHS.Val == RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000623 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000624 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000625 case tok::amp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000626 Res = LHS.Val & RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000627 break;
628 case tok::caret:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000629 Res = LHS.Val ^ RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000630 break;
631 case tok::pipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000632 Res = LHS.Val | RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000633 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000634 case tok::ampamp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000635 Res = (LHS.Val != 0 && RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000636 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000637 break;
638 case tok::pipepipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000639 Res = (LHS.Val != 0 || RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000640 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000641 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000642 case tok::comma:
Chris Lattnerd89e4582008-05-04 18:36:18 +0000643 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
644 // if not being evaluated.
645 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000646 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
647 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000648 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump11289f42009-09-09 15:08:12 +0000649 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000650 case tok::question: {
651 // Parse the : part of the expression.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000652 if (PeekTok.isNot(tok::colon)) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000653 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
654 << LHS.getRange(), RHS.getRange();
Chris Lattner03c40412008-11-23 23:17:07 +0000655 PP.Diag(OpLoc, diag::note_matching) << "?";
Chris Lattner22eb9722006-06-18 05:43:12 +0000656 return true;
657 }
658 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000659 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000660
661 // Evaluate the value after the :.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000662 bool AfterColonLive = ValueLive && LHS.Val == 0;
663 PPValue AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000664 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000665 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
666 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000667
Chris Lattnerca2b3182008-05-05 20:09:27 +0000668 // Parse anything after the : with the same precedence as ?. We allow
669 // things of equal precedence because ?: is right associative.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000670 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Chris Lattner86054a92007-04-10 05:26:38 +0000671 PeekTok, AfterColonLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000672 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000673
Chris Lattner22eb9722006-06-18 05:43:12 +0000674 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000675 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
676 RHS.setEnd(AfterColonVal.getRange().getEnd());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000677
678 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
679 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000680 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattner22eb9722006-06-18 05:43:12 +0000682 // Figure out the precedence of the token after the : part.
683 PeekPrec = getPrecedence(PeekTok.getKind());
684 break;
685 }
686 case tok::colon:
687 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere05c4df2008-11-18 21:48:13 +0000688 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
689 << LHS.getRange() << RHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000690 return true;
691 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000692
693 // If this operator is live and overflowed, report the issue.
694 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000695 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
696 << LHS.getRange() << RHS.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000697
Chris Lattner9cc755d2007-04-10 07:07:11 +0000698 // Put the result back into 'LHS' for our next iteration.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000699 LHS.Val = Res;
700 LHS.setEnd(RHS.getRange().getEnd());
Chris Lattner22eb9722006-06-18 05:43:12 +0000701 }
Mike Stump11289f42009-09-09 15:08:12 +0000702
Chris Lattner22eb9722006-06-18 05:43:12 +0000703 return false;
704}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000705
706/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
707/// may occur after a #if or #elif directive. If the expression is equivalent
708/// to "!defined(X)" return X in IfNDefMacro.
709bool Preprocessor::
710EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattner4c53c402009-12-14 05:00:18 +0000711 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
712 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
713 // in which case a directive is undefined behavior. We want macros to be able
714 // to recursively expand in order to get more gcc-list behavior, so we force
715 // DisableMacroExpansion to false and restore it when we're done parsing the
716 // expression.
717 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
718 DisableMacroExpansion = false;
719
Chris Lattnere3519cc2006-07-04 18:11:39 +0000720 // Peek ahead one token.
Chris Lattner146762e2007-07-20 16:59:19 +0000721 Token Tok;
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000722 LexNonComment(Tok);
Douglas Gregor12785102010-08-24 20:21:13 +0000723
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000724 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner37e05872008-03-05 18:54:05 +0000725 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattner3565c8e2008-05-05 06:45:50 +0000727 PPValue ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000728 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000729 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000730 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000731 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000732 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000733
734 // Restore 'DisableMacroExpansion'.
735 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000736 return false;
737 }
Mike Stump11289f42009-09-09 15:08:12 +0000738
Chris Lattnere3519cc2006-07-04 18:11:39 +0000739 // If we are at the end of the expression after just parsing a value, there
740 // must be no (unparenthesized) binary operators involved, so we can exit
741 // directly.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000742 if (Tok.is(tok::eod)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000743 // If the expression we parsed was of the form !defined(macro), return the
744 // macro in IfNDefMacro.
745 if (DT.State == DefinedTracker::NotDefinedMacro)
746 IfNDefMacro = DT.TheMacro;
Mike Stump11289f42009-09-09 15:08:12 +0000747
Chris Lattner4c53c402009-12-14 05:00:18 +0000748 // Restore 'DisableMacroExpansion'.
749 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000750 return ResVal.Val != 0;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000751 }
Mike Stump11289f42009-09-09 15:08:12 +0000752
Chris Lattnere3519cc2006-07-04 18:11:39 +0000753 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
754 // operator and the stuff after it.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000755 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
756 Tok, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000757 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000758 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000759 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000760
761 // Restore 'DisableMacroExpansion'.
762 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000763 return false;
764 }
Mike Stump11289f42009-09-09 15:08:12 +0000765
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000766 // If we aren't at the tok::eod token, something bad happened, like an extra
Chris Lattnere3519cc2006-07-04 18:11:39 +0000767 // ')' token.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000768 if (Tok.isNot(tok::eod)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000769 Diag(Tok, diag::err_pp_expected_eol);
770 DiscardUntilEndOfDirective();
771 }
Mike Stump11289f42009-09-09 15:08:12 +0000772
Chris Lattner4c53c402009-12-14 05:00:18 +0000773 // Restore 'DisableMacroExpansion'.
774 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000775 return ResVal.Val != 0;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000776}
777