blob: 12f5084298df28c2ff5df6d86dc66d0515660140 [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"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000020#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/SourceLocation.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner81278c62006-10-14 19:03:49 +000023#include "clang/Basic/TargetInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000024#include "clang/Basic/TokenKinds.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/Lex/CodeCompletionHandler.h"
Chris Lattner60f36222009-01-29 05:15:15 +000026#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "clang/Lex/LiteralSupport.h"
28#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000029#include "clang/Lex/PPCallbacks.h"
30#include "clang/Lex/Token.h"
Chris Lattnera9eac7f2007-04-05 05:24:00 +000031#include "llvm/ADT/APSInt.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000032#include "llvm/ADT/SmallString.h"
33#include "llvm/ADT/StringRef.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000034#include "llvm/Support/ErrorHandling.h"
David Blaikie1dc4a3d2013-03-18 23:22:28 +000035#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000036#include <cassert>
37
Chris Lattner22eb9722006-06-18 05:43:12 +000038using namespace clang;
39
Dan Gohman28ade552010-07-26 21:25:24 +000040namespace {
41
Chris Lattner3565c8e2008-05-05 06:45:50 +000042/// PPValue - Represents the value of a subexpression of a preprocessor
43/// conditional and the source range covered by it.
44class PPValue {
45 SourceRange Range;
Richard Smith4d247e72016-04-16 00:07:09 +000046 IdentifierInfo *II;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000047
Chris Lattner3565c8e2008-05-05 06:45:50 +000048public:
49 llvm::APSInt Val;
Mike Stump11289f42009-09-09 15:08:12 +000050
Chris Lattner3565c8e2008-05-05 06:45:50 +000051 // Default ctor - Construct an 'invalid' PPValue.
52 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump11289f42009-09-09 15:08:12 +000053
Richard Smith4d247e72016-04-16 00:07:09 +000054 // If this value was produced by directly evaluating an identifier, produce
55 // that identifier.
56 IdentifierInfo *getIdentifier() const { return II; }
57 void setIdentifier(IdentifierInfo *II) { this->II = II; }
58
Chris Lattner3565c8e2008-05-05 06:45:50 +000059 unsigned getBitWidth() const { return Val.getBitWidth(); }
60 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump11289f42009-09-09 15:08:12 +000061
Craig Toppere335f252015-10-04 04:53:55 +000062 SourceRange getRange() const { return Range; }
Mike Stump11289f42009-09-09 15:08:12 +000063
Chris Lattner3565c8e2008-05-05 06:45:50 +000064 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
65 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump11289f42009-09-09 15:08:12 +000066 Range.setBegin(B); Range.setEnd(E);
Chris Lattner3565c8e2008-05-05 06:45:50 +000067 }
68 void setBegin(SourceLocation L) { Range.setBegin(L); }
69 void setEnd(SourceLocation L) { Range.setEnd(L); }
70};
71
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000072} // end anonymous namespace
Dan Gohman28ade552010-07-26 21:25:24 +000073
Chris Lattner3565c8e2008-05-05 06:45:50 +000074static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +000075 Token &PeekTok, bool ValueLive,
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +000076 bool &IncludedUndefinedIds,
Chris Lattner86054a92007-04-10 05:26:38 +000077 Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000078
Chris Lattnerb9d90f72006-07-04 18:32:03 +000079/// DefinedTracker - This struct is used while parsing expressions to keep track
80/// of whether !defined(X) has been seen.
81///
82/// With this simple scheme, we handle the basic forms:
83/// !defined(X) and !defined X
84/// but we also trivially handle (silly) stuff like:
85/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
86struct DefinedTracker {
87 /// Each time a Value is evaluated, it returns information about whether the
88 /// parsed value is of the form defined(X), !defined(X) or is something else.
89 enum TrackerState {
90 DefinedMacro, // defined(X)
91 NotDefinedMacro, // !defined(X)
92 Unknown // Something else.
93 } State;
94 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
95 /// indicates the macro that was checked.
96 IdentifierInfo *TheMacro;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +000097 bool IncludedUndefinedIds = false;
Chris Lattnerb9d90f72006-07-04 18:32:03 +000098};
99
John Thompsonb5353522009-10-30 13:49:06 +0000100/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattner4c53c402009-12-14 05:00:18 +0000101static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
102 bool ValueLive, Preprocessor &PP) {
John Thompsoncda95fe2013-07-19 18:50:04 +0000103 SourceLocation beginLoc(PeekTok.getLocation());
104 Result.setBegin(beginLoc);
John Thompsonb5353522009-10-30 13:49:06 +0000105
106 // Get the next token, don't expand it.
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000107 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000108
109 // Two options, it can either be a pp-identifier or a (.
110 SourceLocation LParenLoc;
111 if (PeekTok.is(tok::l_paren)) {
112 // Found a paren, remember we saw it and skip it.
113 LParenLoc = PeekTok.getLocation();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000114 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000115 }
116
Douglas Gregor12785102010-08-24 20:21:13 +0000117 if (PeekTok.is(tok::code_completion)) {
118 if (PP.getCodeCompletionHandler())
119 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000120 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000121 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000122 }
Alp Tokerb05e0b52014-05-21 06:13:51 +0000123
John Thompsonb5353522009-10-30 13:49:06 +0000124 // If we don't have a pp-identifier now, this is an error.
Serge Pavlovd024f522014-10-24 17:31:32 +0000125 if (PP.CheckMacroName(PeekTok, MU_Other))
John Thompsonb5353522009-10-30 13:49:06 +0000126 return true;
John Thompsonb5353522009-10-30 13:49:06 +0000127
128 // Otherwise, we got an identifier, is it defined to something?
Alp Tokerb05e0b52014-05-21 06:13:51 +0000129 IdentifierInfo *II = PeekTok.getIdentifierInfo();
Richard Smith66a81862015-05-04 02:25:31 +0000130 MacroDefinition Macro = PP.getMacroDefinition(II);
Richard Smith20e883e2015-04-29 23:20:19 +0000131 Result.Val = !!Macro;
Richard Smith66a81862015-05-04 02:25:31 +0000132 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000133 DT.IncludedUndefinedIds = !Macro;
John Thompsonb5353522009-10-30 13:49:06 +0000134
135 // If there is a macro, mark it used.
Richard Smith20e883e2015-04-29 23:20:19 +0000136 if (Result.Val != 0 && ValueLive)
137 PP.markMacroAsUsed(Macro.getMacroInfo());
John Thompsonb5353522009-10-30 13:49:06 +0000138
John Thompsoncda95fe2013-07-19 18:50:04 +0000139 // Save macro token for callback.
140 Token macroToken(PeekTok);
Douglas Gregor82e0d7282011-10-14 00:49:43 +0000141
John Thompsonb5353522009-10-30 13:49:06 +0000142 // If we are in parens, ensure we have a trailing ).
143 if (LParenLoc.isValid()) {
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000144 // Consume identifier.
145 Result.setEnd(PeekTok.getLocation());
146 PP.LexUnexpandedNonComment(PeekTok);
147
John Thompsonb5353522009-10-30 13:49:06 +0000148 if (PeekTok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +0000149 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
150 << "'defined'" << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +0000151 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
John Thompsonb5353522009-10-30 13:49:06 +0000152 return true;
153 }
154 // Consume the ).
155 Result.setEnd(PeekTok.getLocation());
156 PP.LexNonComment(PeekTok);
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000157 } else {
158 // Consume identifier.
159 Result.setEnd(PeekTok.getLocation());
160 PP.LexNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000161 }
162
Nico Weberb2348f42016-01-19 15:15:31 +0000163 // [cpp.cond]p4:
164 // Prior to evaluation, macro invocations in the list of preprocessing
165 // tokens that will become the controlling constant expression are replaced
166 // (except for those macro names modified by the 'defined' unary operator),
167 // just as in normal text. If the token 'defined' is generated as a result
168 // of this replacement process or use of the 'defined' unary operator does
169 // not match one of the two specified forms prior to macro replacement, the
170 // behavior is undefined.
171 // This isn't an idle threat, consider this program:
172 // #define FOO
173 // #define BAR defined(FOO)
174 // #if BAR
175 // ...
176 // #else
177 // ...
178 // #endif
179 // clang and gcc will pick the #if branch while Visual Studio will take the
180 // #else branch. Emit a warning about this undefined behavior.
181 if (beginLoc.isMacroID()) {
182 bool IsFunctionTypeMacro =
183 PP.getSourceManager()
184 .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
185 .getExpansion()
186 .isFunctionMacroExpansion();
187 // For object-type macros, it's easy to replace
188 // #define FOO defined(BAR)
189 // with
190 // #if defined(BAR)
191 // #define FOO 1
192 // #else
193 // #define FOO 0
194 // #endif
195 // and doing so makes sense since compilers handle this differently in
196 // practice (see example further up). But for function-type macros,
197 // there is no good way to write
198 // # define FOO(x) (defined(M_ ## x) && M_ ## x)
199 // in a different way, and compilers seem to agree on how to behave here.
200 // So warn by default on object-type macros, but only warn in -pedantic
201 // mode on function-type macros.
202 if (IsFunctionTypeMacro)
203 PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
204 else
205 PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
206 }
207
John Thompsoncda95fe2013-07-19 18:50:04 +0000208 // Invoke the 'defined' callback.
209 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
Richard Smith36bd40d2015-05-04 03:15:40 +0000210 Callbacks->Defined(macroToken, Macro,
John Thompsoncda95fe2013-07-19 18:50:04 +0000211 SourceRange(beginLoc, PeekTok.getLocation()));
212 }
213
John Thompsonb5353522009-10-30 13:49:06 +0000214 // Success, remember that we saw defined(X).
215 DT.State = DefinedTracker::DefinedMacro;
216 DT.TheMacro = II;
217 return false;
218}
219
Chris Lattner22eb9722006-06-18 05:43:12 +0000220/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
221/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000222/// parsing. This function also returns information about the form of the
223/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +0000224///
225/// If ValueLive is false, then this value is being evaluated in a context where
226/// the result is not used. As such, avoid diagnostics that relate to
227/// evaluation.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000228static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
229 bool ValueLive, Preprocessor &PP) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000230 DT.State = DefinedTracker::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000231
Richard Smith4d247e72016-04-16 00:07:09 +0000232 Result.setIdentifier(nullptr);
233
Douglas Gregorec00a262010-08-24 22:20:20 +0000234 if (PeekTok.is(tok::code_completion)) {
235 if (PP.getCodeCompletionHandler())
236 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000237 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000238 PP.LexNonComment(PeekTok);
Douglas Gregorec00a262010-08-24 22:20:20 +0000239 }
240
Chris Lattner22eb9722006-06-18 05:43:12 +0000241 // If this token's spelling is a pp-identifier, check to see if it is
242 // 'defined' or if it is a macro. Note that we check here because many
243 // keywords are pp-identifiers, so we can't check the kind.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000244 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner676268e2009-12-14 04:26:45 +0000245 // Handle "defined X" and "defined(X)".
246 if (II->isStr("defined"))
Nico Weberb2348f42016-01-19 15:15:31 +0000247 return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
248
Chris Lattner676268e2009-12-14 04:26:45 +0000249 // If this identifier isn't 'defined' or one of the special
250 // preprocessor keywords and it wasn't macro expanded, it turns
251 // into a simple 0, unless it is the C++ keyword "true", in which case it
252 // turns into "1".
Eli Friedman847f3ca2012-09-20 02:38:38 +0000253 if (ValueLive &&
254 II->getTokenID() != tok::kw_true &&
255 II->getTokenID() != tok::kw_false)
Chris Lattner676268e2009-12-14 04:26:45 +0000256 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
257 Result.Val = II->getTokenID() == tok::kw_true;
258 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
Richard Smith4d247e72016-04-16 00:07:09 +0000259 Result.setIdentifier(II);
Chris Lattner676268e2009-12-14 04:26:45 +0000260 Result.setRange(PeekTok.getLocation());
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000261 DT.IncludedUndefinedIds = (II->getTokenID() != tok::kw_true &&
262 II->getTokenID() != tok::kw_false);
Chris Lattner676268e2009-12-14 04:26:45 +0000263 PP.LexNonComment(PeekTok);
264 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000265 }
Mike Stump11289f42009-09-09 15:08:12 +0000266
Chris Lattner22eb9722006-06-18 05:43:12 +0000267 switch (PeekTok.getKind()) {
268 default: // Non-value token.
Chris Lattnerf8f94542008-04-13 20:38:43 +0000269 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000270 return true;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000271 case tok::eod:
Chris Lattner22eb9722006-06-18 05:43:12 +0000272 case tok::r_paren:
273 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000274 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000275 return true;
276 case tok::numeric_constant: {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000277 SmallString<64> IntegerBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000278 bool NumberInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000279 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000280 &NumberInvalid);
281 if (NumberInvalid)
282 return true; // a diagnostic was already reported
283
Dmitri Gribenko7ba91722012-09-24 09:53:54 +0000284 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000285 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000286 return true; // a diagnostic was already reported.
Mike Stump11289f42009-09-09 15:08:12 +0000287
Chris Lattnered045422007-08-26 03:29:23 +0000288 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000289 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000290 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000291 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000292 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000293
Richard Smith39570d002012-03-08 08:45:32 +0000294 // Complain about, and drop, any ud-suffix.
295 if (Literal.hasUDSuffix())
296 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
297
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000298 // 'long long' is a C99 or C++11 feature.
299 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
300 if (PP.getLangOpts().CPlusPlus)
301 PP.Diag(PeekTok,
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000302 PP.getLangOpts().CPlusPlus11 ?
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000303 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
304 else
305 PP.Diag(PeekTok, diag::ext_c99_longlong);
306 }
Neil Boothac582c52007-08-29 22:00:19 +0000307
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000308 // Parse the integer literal into Result.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000309 if (Literal.GetIntegerValue(Result.Val)) {
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000310 // Overflow parsing integer literal.
Aaron Ballman446867e2014-07-22 14:08:09 +0000311 if (ValueLive)
Aaron Ballman31f42312014-07-24 14:51:23 +0000312 PP.Diag(PeekTok, diag::err_integer_literal_too_large)
313 << /* Unsigned */ 1;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000314 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000315 } else {
316 // Set the signedness of the result to match whether there was a U suffix
317 // or not.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000318 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump11289f42009-09-09 15:08:12 +0000319
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000320 // Detect overflow based on whether the value is signed. If signed
321 // and if the value is too large, emit a warning "integer constant is so
322 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
323 // is 64-bits.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000324 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Richard Smith7e34fbd2014-03-14 21:21:24 +0000325 // Octal, hexadecimal, and binary literals are implicitly unsigned if
326 // the value does not fit into a signed integer type.
Eli Friedman088d39a2013-07-23 00:25:18 +0000327 if (ValueLive && Literal.getRadix() == 10)
Aaron Ballman31f42312014-07-24 14:51:23 +0000328 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000329 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000330 }
331 }
Mike Stump11289f42009-09-09 15:08:12 +0000332
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000333 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000334 Result.setRange(PeekTok.getLocation());
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000335 PP.LexNonComment(PeekTok);
336 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000337 }
Douglas Gregorfb65e592011-07-27 05:40:30 +0000338 case tok::char_constant: // 'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000339 case tok::wide_char_constant: // L'x'
Richard Smith3e3a7052014-11-08 06:08:42 +0000340 case tok::utf8_char_constant: // u8'x'
Douglas Gregorfb65e592011-07-27 05:40:30 +0000341 case tok::utf16_char_constant: // u'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000342 case tok::utf32_char_constant: { // U'x'
Richard Smithd67aea22012-03-06 03:21:47 +0000343 // Complain about, and drop, any ud-suffix.
344 if (PeekTok.hasUDSuffix())
Richard Smith39570d002012-03-08 08:45:32 +0000345 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smithd67aea22012-03-06 03:21:47 +0000346
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000347 SmallString<32> CharBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000348 bool CharInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000349 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000350 if (CharInvalid)
351 return true;
Benjamin Kramer0a1abd42010-02-27 13:44:12 +0000352
353 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregorfb65e592011-07-27 05:40:30 +0000354 PeekTok.getLocation(), PP, PeekTok.getKind());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000355 if (Literal.hadError())
356 return true; // A diagnostic was already emitted.
357
358 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar1b444192009-11-13 05:51:54 +0000359 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedmand8cec572009-06-01 05:25:02 +0000360 unsigned NumBits;
361 if (Literal.isMultiChar())
362 NumBits = TI.getIntWidth();
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000363 else if (Literal.isWide())
364 NumBits = TI.getWCharWidth();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000365 else if (Literal.isUTF16())
366 NumBits = TI.getChar16Width();
367 else if (Literal.isUTF32())
368 NumBits = TI.getChar32Width();
Eli Friedmand8cec572009-06-01 05:25:02 +0000369 else
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000370 NumBits = TI.getCharWidth();
Eli Friedmand8cec572009-06-01 05:25:02 +0000371
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000372 // Set the width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000373 llvm::APSInt Val(NumBits);
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000374 // Set the value.
375 Val = Literal.getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000376 // Set the signedness. UTF-16 and UTF-32 are always unsigned
Michael Wong867eeb52015-02-24 13:34:20 +0000377 if (Literal.isWide())
378 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
379 else if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikiebbafb8a2012-03-11 07:00:24 +0000380 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000381
Chris Lattner3565c8e2008-05-05 06:45:50 +0000382 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
383 Result.Val = Val.extend(Result.Val.getBitWidth());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000384 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000385 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000386 "intmax_t smaller than char/wchar_t?");
Chris Lattner3565c8e2008-05-05 06:45:50 +0000387 Result.Val = Val;
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000388 }
389
390 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000391 Result.setRange(PeekTok.getLocation());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000392 PP.LexNonComment(PeekTok);
393 return false;
394 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000395 case tok::l_paren: {
396 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000397 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000398 // Parse the value and if there are any binary operators involved, parse
399 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000400 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000401
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000402 // If this is a silly value like (X), which doesn't need parens, check for
403 // !(defined X).
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000404 if (PeekTok.is(tok::r_paren)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000405 // Just use DT unmodified as our result.
406 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000407 // Otherwise, we have something like (x+y), and we consumed '(x'.
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000408 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
409 DT.IncludedUndefinedIds, PP))
Chris Lattner86054a92007-04-10 05:26:38 +0000410 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000412 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000413 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
414 << Result.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000415 PP.Diag(Start, diag::note_matching) << tok::l_paren;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000416 return true;
417 }
418 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000419 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000420 Result.setRange(Start, PeekTok.getLocation());
Richard Smith4d247e72016-04-16 00:07:09 +0000421 Result.setIdentifier(nullptr);
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000422 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000423 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000424 }
425 case tok::plus: {
426 SourceLocation Start = PeekTok.getLocation();
Chris Lattner22eb9722006-06-18 05:43:12 +0000427 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000428 PP.LexNonComment(PeekTok);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000429 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
430 Result.setBegin(Start);
Richard Smith4d247e72016-04-16 00:07:09 +0000431 Result.setIdentifier(nullptr);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000432 return false;
433 }
Chris Lattner7e61ac52007-04-11 03:42:36 +0000434 case tok::minus: {
435 SourceLocation Loc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000436 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000437 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000438 Result.setBegin(Loc);
Richard Smith4d247e72016-04-16 00:07:09 +0000439 Result.setIdentifier(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000441 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000442 Result.Val = -Result.Val;
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattner1cb0e612008-07-03 03:47:30 +0000444 // -MININT is the only thing that overflows. Unsigned never overflows.
445 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattner7e61ac52007-04-11 03:42:36 +0000447 // If this operator is live and overflowed, report the issue.
448 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000449 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000451 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000452 return false;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Chris Lattner3565c8e2008-05-05 06:45:50 +0000455 case tok::tilde: {
456 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000457 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000458 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000459 Result.setBegin(Start);
Richard Smith4d247e72016-04-16 00:07:09 +0000460 Result.setIdentifier(nullptr);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000461
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000462 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000463 Result.Val = ~Result.Val;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000464 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000465 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattner3565c8e2008-05-05 06:45:50 +0000468 case tok::exclaim: {
469 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000470 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000471 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000472 Result.setBegin(Start);
473 Result.Val = !Result.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000474 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000475 Result.Val.setIsUnsigned(false);
Richard Smith4d247e72016-04-16 00:07:09 +0000476 Result.setIdentifier(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000477
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000478 if (DT.State == DefinedTracker::DefinedMacro)
479 DT.State = DefinedTracker::NotDefinedMacro;
480 else if (DT.State == DefinedTracker::NotDefinedMacro)
481 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000482 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Chris Lattner22eb9722006-06-18 05:43:12 +0000485 // FIXME: Handle #assert
486 }
487}
488
Chris Lattner22eb9722006-06-18 05:43:12 +0000489/// getPrecedence - Return the precedence of the specified binary operator
490/// token. This returns:
491/// ~0 - Invalid token.
Chris Lattner3c57f7e2008-05-05 04:10:51 +0000492/// 14 -> 3 - various operators.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000493/// 0 - 'eod' or ')'
Chris Lattner22eb9722006-06-18 05:43:12 +0000494static unsigned getPrecedence(tok::TokenKind Kind) {
495 switch (Kind) {
496 default: return ~0U;
497 case tok::percent:
498 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000499 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000500 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000501 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000502 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000503 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000504 case tok::lessequal:
505 case tok::less:
506 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000507 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000508 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000509 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000510 case tok::amp: return 9;
511 case tok::caret: return 8;
512 case tok::pipe: return 7;
513 case tok::ampamp: return 6;
514 case tok::pipepipe: return 5;
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000515 case tok::question: return 4;
516 case tok::comma: return 3;
Chris Lattnerd89e4582008-05-04 18:36:18 +0000517 case tok::colon: return 2;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000518 case tok::r_paren: return 0;// Lowest priority, end of expr.
519 case tok::eod: return 0;// Lowest priority, end of directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000520 }
521}
522
Richard Smith4d247e72016-04-16 00:07:09 +0000523static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
524 Token &Tok) {
525 if (Tok.is(tok::l_paren) && LHS.getIdentifier())
526 PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
527 << LHS.getIdentifier();
528 else
529 PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
530 << LHS.getRange();
531}
Chris Lattner22eb9722006-06-18 05:43:12 +0000532
533/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner3565c8e2008-05-05 06:45:50 +0000534/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Chris Lattner86054a92007-04-10 05:26:38 +0000535///
536/// If ValueLive is false, then this value is being evaluated in a context where
537/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner3565c8e2008-05-05 06:45:50 +0000538/// evaluation, such as division by zero warnings.
539static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +0000540 Token &PeekTok, bool ValueLive,
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000541 bool &IncludedUndefinedIds,
Chris Lattner86054a92007-04-10 05:26:38 +0000542 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000543 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
544 // If this token isn't valid, report the error.
545 if (PeekPrec == ~0U) {
Richard Smith4d247e72016-04-16 00:07:09 +0000546 diagnoseUnexpectedOperator(PP, LHS, PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000547 return true;
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000550 while (true) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000551 // If this token has a lower precedence than we are allowed to parse, return
552 // it so that higher levels of the recursion can parse it.
553 if (PeekPrec < MinPrec)
554 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000555
Chris Lattner22eb9722006-06-18 05:43:12 +0000556 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump11289f42009-09-09 15:08:12 +0000557
Chris Lattner86054a92007-04-10 05:26:38 +0000558 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump11289f42009-09-09 15:08:12 +0000559 // dead. Note that this cannot just clobber ValueLive. Consider
Chris Lattner86054a92007-04-10 05:26:38 +0000560 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
561 // this example, the RHS of the && being dead does not make the rest of the
562 // expr dead.
563 bool RHSIsLive;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000564 if (Operator == tok::ampamp && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000565 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000566 else if (Operator == tok::pipepipe && LHS.Val != 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000567 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000568 else if (Operator == tok::question && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000569 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
570 else
571 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000572
Chris Lattner3565c8e2008-05-05 06:45:50 +0000573 // Consume the operator, remembering the operator's location for reporting.
574 SourceLocation OpLoc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000575 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000576
Chris Lattner3565c8e2008-05-05 06:45:50 +0000577 PPValue RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000578 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000579 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000580 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000581 IncludedUndefinedIds = DT.IncludedUndefinedIds;
Chris Lattner22eb9722006-06-18 05:43:12 +0000582
583 // Remember the precedence of this operator and get the precedence of the
584 // operator immediately to the right of the RHS.
585 unsigned ThisPrec = PeekPrec;
586 PeekPrec = getPrecedence(PeekTok.getKind());
587
588 // If this token isn't valid, report the error.
589 if (PeekPrec == ~0U) {
Richard Smith4d247e72016-04-16 00:07:09 +0000590 diagnoseUnexpectedOperator(PP, RHS, PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000591 return true;
592 }
Mike Stump11289f42009-09-09 15:08:12 +0000593
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000594 // Decide whether to include the next binop in this subexpression. For
595 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattnerca2b3182008-05-05 20:09:27 +0000596 // handle y*z as a single subexpression. We do this because the precedence
597 // of * is higher than that of +. The only strange case we have to handle
598 // here is for the ?: operator, where the precedence is actually lower than
599 // the LHS of the '?'. The grammar rule is:
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000600 //
601 // conditional-expression ::=
602 // logical-OR-expression ? expression : conditional-expression
603 // where 'expression' is actually comma-expression.
604 unsigned RHSPrec;
605 if (Operator == tok::question)
606 // The RHS of "?" should be maximally consumed as an expression.
607 RHSPrec = getPrecedence(tok::comma);
608 else // All others should munch while higher precedence.
609 RHSPrec = ThisPrec+1;
Mike Stump11289f42009-09-09 15:08:12 +0000610
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000611 if (PeekPrec >= RHSPrec) {
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000612 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
613 IncludedUndefinedIds, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000614 return true;
615 PeekPrec = getPrecedence(PeekTok.getKind());
616 }
617 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump11289f42009-09-09 15:08:12 +0000618
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000619 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump11289f42009-09-09 15:08:12 +0000620 // either operand is unsigned.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000621 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattnerca671b02008-05-04 23:46:17 +0000622 switch (Operator) {
623 case tok::question: // No UAC for x and y in "x ? y : z".
624 case tok::lessless: // Shift amount doesn't UAC with shift value.
625 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
626 case tok::comma: // Comma operands are not subject to UACs.
627 case tok::pipepipe: // Logical || does not do UACs.
628 case tok::ampamp: // Logical && does not do UACs.
629 break; // No UAC
630 default:
Chris Lattner99ca0912007-04-11 04:14:45 +0000631 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
632 // If this just promoted something from signed to unsigned, and if the
633 // value was negative, warn about it.
634 if (ValueLive && Res.isUnsigned()) {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000635 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Craig Topper7f5ff212015-11-14 02:09:55 +0000636 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
Chris Lattnere05c4df2008-11-18 21:48:13 +0000637 << LHS.Val.toString(10, true) + " to " +
638 LHS.Val.toString(10, false)
639 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000640 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Craig Topper7f5ff212015-11-14 02:09:55 +0000641 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
Chris Lattnere05c4df2008-11-18 21:48:13 +0000642 << RHS.Val.toString(10, true) + " to " +
643 RHS.Val.toString(10, false)
644 << LHS.getRange() << RHS.getRange();
Chris Lattner99ca0912007-04-11 04:14:45 +0000645 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000646 LHS.Val.setIsUnsigned(Res.isUnsigned());
647 RHS.Val.setIsUnsigned(Res.isUnsigned());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000648 }
Mike Stump11289f42009-09-09 15:08:12 +0000649
Chris Lattner5a0f1642007-04-10 06:54:33 +0000650 bool Overflow = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000651 switch (Operator) {
David Blaikie83d382b2011-09-23 05:06:16 +0000652 default: llvm_unreachable("Unknown operator token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000653 case tok::percent:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000654 if (RHS.Val != 0)
655 Res = LHS.Val % RHS.Val;
656 else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000657 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
658 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000659 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000660 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000661 break;
662 case tok::slash:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000663 if (RHS.Val != 0) {
Chris Lattner2edb9262010-10-13 23:46:56 +0000664 if (LHS.Val.isSigned())
665 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
666 else
667 Res = LHS.Val / RHS.Val;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000668 } else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000669 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
670 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000671 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000672 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000673 break;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000675 case tok::star:
Chris Lattner2edb9262010-10-13 23:46:56 +0000676 if (Res.isSigned())
677 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
678 else
679 Res = LHS.Val * RHS.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000680 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000681 case tok::lessless: {
682 // Determine whether overflow is about to happen.
David Majnemer3e8b6ac2014-10-13 22:18:22 +0000683 if (LHS.isUnsigned())
684 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
685 else
686 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000687 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000688 }
689 case tok::greatergreater: {
690 // Determine whether overflow is about to happen.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000691 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Richard Trieucc3949d2016-02-18 22:34:54 +0000692 if (ShAmt >= LHS.getBitWidth()) {
693 Overflow = true;
694 ShAmt = LHS.getBitWidth()-1;
695 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000696 Res = LHS.Val >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000697 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000698 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000699 case tok::plus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000700 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000701 Res = LHS.Val + RHS.Val;
702 else
703 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000704 break;
705 case tok::minus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000706 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000707 Res = LHS.Val - RHS.Val;
708 else
709 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000710 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000711 case tok::lessequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000712 Res = LHS.Val <= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000713 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000714 break;
715 case tok::less:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000716 Res = LHS.Val < RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000717 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000718 break;
719 case tok::greaterequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000720 Res = LHS.Val >= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000721 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000722 break;
723 case tok::greater:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000724 Res = LHS.Val > RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000725 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000726 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000727 case tok::exclaimequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000728 Res = LHS.Val != RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000729 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000730 break;
731 case tok::equalequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000732 Res = LHS.Val == RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000733 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000734 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000735 case tok::amp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000736 Res = LHS.Val & RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000737 break;
738 case tok::caret:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000739 Res = LHS.Val ^ RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000740 break;
741 case tok::pipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000742 Res = LHS.Val | RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000743 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000744 case tok::ampamp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000745 Res = (LHS.Val != 0 && RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000746 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000747 break;
748 case tok::pipepipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000749 Res = (LHS.Val != 0 || RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000750 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000751 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000752 case tok::comma:
Chris Lattnerd89e4582008-05-04 18:36:18 +0000753 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
754 // if not being evaluated.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000755 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000756 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
757 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000758 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump11289f42009-09-09 15:08:12 +0000759 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000760 case tok::question: {
761 // Parse the : part of the expression.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000762 if (PeekTok.isNot(tok::colon)) {
Alp Toker35d87032013-12-30 23:29:50 +0000763 PP.Diag(PeekTok.getLocation(), diag::err_expected)
764 << tok::colon << LHS.getRange() << RHS.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000765 PP.Diag(OpLoc, diag::note_matching) << tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +0000766 return true;
767 }
768 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000769 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000770
771 // Evaluate the value after the :.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000772 bool AfterColonLive = ValueLive && LHS.Val == 0;
773 PPValue AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000774 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000775 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
776 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000777
Chris Lattnerca2b3182008-05-05 20:09:27 +0000778 // Parse anything after the : with the same precedence as ?. We allow
779 // things of equal precedence because ?: is right associative.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000780 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000781 PeekTok, AfterColonLive,
782 IncludedUndefinedIds, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000783 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000784
Chris Lattner22eb9722006-06-18 05:43:12 +0000785 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000786 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
787 RHS.setEnd(AfterColonVal.getRange().getEnd());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000788
789 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
790 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000791 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump11289f42009-09-09 15:08:12 +0000792
Chris Lattner22eb9722006-06-18 05:43:12 +0000793 // Figure out the precedence of the token after the : part.
794 PeekPrec = getPrecedence(PeekTok.getKind());
795 break;
796 }
797 case tok::colon:
798 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere05c4df2008-11-18 21:48:13 +0000799 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
800 << LHS.getRange() << RHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000801 return true;
802 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000803
804 // If this operator is live and overflowed, report the issue.
805 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000806 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
807 << LHS.getRange() << RHS.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000808
Chris Lattner9cc755d2007-04-10 07:07:11 +0000809 // Put the result back into 'LHS' for our next iteration.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000810 LHS.Val = Res;
811 LHS.setEnd(RHS.getRange().getEnd());
Richard Smith4d247e72016-04-16 00:07:09 +0000812 RHS.setIdentifier(nullptr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000813 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000814}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000815
816/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
817/// may occur after a #if or #elif directive. If the expression is equivalent
818/// to "!defined(X)" return X in IfNDefMacro.
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000819Preprocessor::DirectiveEvalResult
820Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
David Blaikie1dc4a3d2013-03-18 23:22:28 +0000821 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
Chris Lattner4c53c402009-12-14 05:00:18 +0000822 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
823 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
824 // in which case a directive is undefined behavior. We want macros to be able
825 // to recursively expand in order to get more gcc-list behavior, so we force
826 // DisableMacroExpansion to false and restore it when we're done parsing the
827 // expression.
828 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
829 DisableMacroExpansion = false;
830
Chris Lattnere3519cc2006-07-04 18:11:39 +0000831 // Peek ahead one token.
Chris Lattner146762e2007-07-20 16:59:19 +0000832 Token Tok;
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000833 LexNonComment(Tok);
Douglas Gregor12785102010-08-24 20:21:13 +0000834
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000835 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner37e05872008-03-05 18:54:05 +0000836 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattner3565c8e2008-05-05 06:45:50 +0000838 PPValue ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000839 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000840 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000841 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000842 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000843 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000844
845 // Restore 'DisableMacroExpansion'.
846 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000847 return {false, DT.IncludedUndefinedIds};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000848 }
Mike Stump11289f42009-09-09 15:08:12 +0000849
Chris Lattnere3519cc2006-07-04 18:11:39 +0000850 // If we are at the end of the expression after just parsing a value, there
851 // must be no (unparenthesized) binary operators involved, so we can exit
852 // directly.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000853 if (Tok.is(tok::eod)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000854 // If the expression we parsed was of the form !defined(macro), return the
855 // macro in IfNDefMacro.
856 if (DT.State == DefinedTracker::NotDefinedMacro)
857 IfNDefMacro = DT.TheMacro;
Mike Stump11289f42009-09-09 15:08:12 +0000858
Chris Lattner4c53c402009-12-14 05:00:18 +0000859 // Restore 'DisableMacroExpansion'.
860 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000861 return {ResVal.Val != 0, DT.IncludedUndefinedIds};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
Chris Lattnere3519cc2006-07-04 18:11:39 +0000864 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
865 // operator and the stuff after it.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000866 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000867 Tok, true, DT.IncludedUndefinedIds, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000868 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000869 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000870 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000871
872 // Restore 'DisableMacroExpansion'.
873 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000874 return {false, DT.IncludedUndefinedIds};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000875 }
Mike Stump11289f42009-09-09 15:08:12 +0000876
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000877 // If we aren't at the tok::eod token, something bad happened, like an extra
Chris Lattnere3519cc2006-07-04 18:11:39 +0000878 // ')' token.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000879 if (Tok.isNot(tok::eod)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000880 Diag(Tok, diag::err_pp_expected_eol);
881 DiscardUntilEndOfDirective();
882 }
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattner4c53c402009-12-14 05:00:18 +0000884 // Restore 'DisableMacroExpansion'.
885 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000886 return {ResVal.Val != 0, DT.IncludedUndefinedIds};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000887}