blob: e5ec2b99f50743c5473958859b3cc5fe9f9d531b [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner22eb9722006-06-18 05:43:12 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattner80965422006-07-04 18:03:19 +00009// This file implements the Preprocessor::EvaluateDirectiveExpression method,
10// which parses and evaluates integer constant expressions for #if directives.
Chris Lattner22eb9722006-06-18 05:43:12 +000011//
12//===----------------------------------------------------------------------===//
13//
Chris Lattner6df79752007-04-04 06:54:19 +000014// FIXME: implement testing for #assert's.
Chris Lattner22eb9722006-06-18 05:43:12 +000015//
16//===----------------------------------------------------------------------===//
17
18#include "clang/Lex/Preprocessor.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000019#include "clang/Basic/IdentifierTable.h"
20#include "clang/Basic/SourceLocation.h"
21#include "clang/Basic/SourceManager.h"
Chris Lattner81278c62006-10-14 19:03:49 +000022#include "clang/Basic/TargetInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000023#include "clang/Basic/TokenKinds.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Lex/CodeCompletionHandler.h"
Chris Lattner60f36222009-01-29 05:15:15 +000025#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/Lex/LiteralSupport.h"
27#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000028#include "clang/Lex/PPCallbacks.h"
29#include "clang/Lex/Token.h"
Chris Lattnera9eac7f2007-04-05 05:24:00 +000030#include "llvm/ADT/APSInt.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000031#include "llvm/ADT/SmallString.h"
32#include "llvm/ADT/StringRef.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000033#include "llvm/Support/ErrorHandling.h"
David Blaikie1dc4a3d2013-03-18 23:22:28 +000034#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000035#include <cassert>
36
Chris Lattner22eb9722006-06-18 05:43:12 +000037using namespace clang;
38
Dan Gohman28ade552010-07-26 21:25:24 +000039namespace {
40
Chris Lattner3565c8e2008-05-05 06:45:50 +000041/// PPValue - Represents the value of a subexpression of a preprocessor
42/// conditional and the source range covered by it.
43class PPValue {
44 SourceRange Range;
Richard Smith4d247e72016-04-16 00:07:09 +000045 IdentifierInfo *II;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000046
Chris Lattner3565c8e2008-05-05 06:45:50 +000047public:
48 llvm::APSInt Val;
Mike Stump11289f42009-09-09 15:08:12 +000049
Chris Lattner3565c8e2008-05-05 06:45:50 +000050 // Default ctor - Construct an 'invalid' PPValue.
51 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump11289f42009-09-09 15:08:12 +000052
Richard Smith4d247e72016-04-16 00:07:09 +000053 // If this value was produced by directly evaluating an identifier, produce
54 // that identifier.
55 IdentifierInfo *getIdentifier() const { return II; }
56 void setIdentifier(IdentifierInfo *II) { this->II = II; }
57
Chris Lattner3565c8e2008-05-05 06:45:50 +000058 unsigned getBitWidth() const { return Val.getBitWidth(); }
59 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump11289f42009-09-09 15:08:12 +000060
Craig Toppere335f252015-10-04 04:53:55 +000061 SourceRange getRange() const { return Range; }
Mike Stump11289f42009-09-09 15:08:12 +000062
Chris Lattner3565c8e2008-05-05 06:45:50 +000063 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
64 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump11289f42009-09-09 15:08:12 +000065 Range.setBegin(B); Range.setEnd(E);
Chris Lattner3565c8e2008-05-05 06:45:50 +000066 }
67 void setBegin(SourceLocation L) { Range.setBegin(L); }
68 void setEnd(SourceLocation L) { Range.setEnd(L); }
69};
70
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000071} // end anonymous namespace
Dan Gohman28ade552010-07-26 21:25:24 +000072
Chris Lattner3565c8e2008-05-05 06:45:50 +000073static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +000074 Token &PeekTok, bool ValueLive,
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +000075 bool &IncludedUndefinedIds,
Chris Lattner86054a92007-04-10 05:26:38 +000076 Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000077
Chris Lattnerb9d90f72006-07-04 18:32:03 +000078/// DefinedTracker - This struct is used while parsing expressions to keep track
79/// of whether !defined(X) has been seen.
80///
81/// With this simple scheme, we handle the basic forms:
82/// !defined(X) and !defined X
83/// but we also trivially handle (silly) stuff like:
84/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
85struct DefinedTracker {
86 /// Each time a Value is evaluated, it returns information about whether the
87 /// parsed value is of the form defined(X), !defined(X) or is something else.
88 enum TrackerState {
89 DefinedMacro, // defined(X)
90 NotDefinedMacro, // !defined(X)
91 Unknown // Something else.
92 } State;
93 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
94 /// indicates the macro that was checked.
95 IdentifierInfo *TheMacro;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +000096 bool IncludedUndefinedIds = false;
Chris Lattnerb9d90f72006-07-04 18:32:03 +000097};
98
John Thompsonb5353522009-10-30 13:49:06 +000099/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattner4c53c402009-12-14 05:00:18 +0000100static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
101 bool ValueLive, Preprocessor &PP) {
John Thompsoncda95fe2013-07-19 18:50:04 +0000102 SourceLocation beginLoc(PeekTok.getLocation());
103 Result.setBegin(beginLoc);
John Thompsonb5353522009-10-30 13:49:06 +0000104
105 // Get the next token, don't expand it.
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000106 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000107
108 // Two options, it can either be a pp-identifier or a (.
109 SourceLocation LParenLoc;
110 if (PeekTok.is(tok::l_paren)) {
111 // Found a paren, remember we saw it and skip it.
112 LParenLoc = PeekTok.getLocation();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000113 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000114 }
115
Douglas Gregor12785102010-08-24 20:21:13 +0000116 if (PeekTok.is(tok::code_completion)) {
117 if (PP.getCodeCompletionHandler())
118 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000119 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000120 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000121 }
Alp Tokerb05e0b52014-05-21 06:13:51 +0000122
John Thompsonb5353522009-10-30 13:49:06 +0000123 // If we don't have a pp-identifier now, this is an error.
Serge Pavlovd024f522014-10-24 17:31:32 +0000124 if (PP.CheckMacroName(PeekTok, MU_Other))
John Thompsonb5353522009-10-30 13:49:06 +0000125 return true;
John Thompsonb5353522009-10-30 13:49:06 +0000126
127 // Otherwise, we got an identifier, is it defined to something?
Alp Tokerb05e0b52014-05-21 06:13:51 +0000128 IdentifierInfo *II = PeekTok.getIdentifierInfo();
Richard Smith66a81862015-05-04 02:25:31 +0000129 MacroDefinition Macro = PP.getMacroDefinition(II);
Richard Smith20e883e2015-04-29 23:20:19 +0000130 Result.Val = !!Macro;
Richard Smith66a81862015-05-04 02:25:31 +0000131 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000132 DT.IncludedUndefinedIds = !Macro;
John Thompsonb5353522009-10-30 13:49:06 +0000133
134 // If there is a macro, mark it used.
Richard Smith20e883e2015-04-29 23:20:19 +0000135 if (Result.Val != 0 && ValueLive)
136 PP.markMacroAsUsed(Macro.getMacroInfo());
John Thompsonb5353522009-10-30 13:49:06 +0000137
John Thompsoncda95fe2013-07-19 18:50:04 +0000138 // Save macro token for callback.
139 Token macroToken(PeekTok);
Douglas Gregor82e0d7282011-10-14 00:49:43 +0000140
John Thompsonb5353522009-10-30 13:49:06 +0000141 // If we are in parens, ensure we have a trailing ).
142 if (LParenLoc.isValid()) {
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000143 // Consume identifier.
144 Result.setEnd(PeekTok.getLocation());
145 PP.LexUnexpandedNonComment(PeekTok);
146
John Thompsonb5353522009-10-30 13:49:06 +0000147 if (PeekTok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +0000148 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
149 << "'defined'" << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +0000150 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
John Thompsonb5353522009-10-30 13:49:06 +0000151 return true;
152 }
153 // Consume the ).
Benjamin Kramerd4d77342019-01-15 17:20:05 +0000154 PP.LexNonComment(PeekTok);
Aaron Ballman65e96bd2019-01-17 20:21:34 +0000155 Result.setEnd(PeekTok.getLocation());
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000156 } else {
157 // Consume identifier.
158 Result.setEnd(PeekTok.getLocation());
159 PP.LexNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000160 }
161
Nico Weberb2348f42016-01-19 15:15:31 +0000162 // [cpp.cond]p4:
163 // Prior to evaluation, macro invocations in the list of preprocessing
164 // tokens that will become the controlling constant expression are replaced
165 // (except for those macro names modified by the 'defined' unary operator),
166 // just as in normal text. If the token 'defined' is generated as a result
167 // of this replacement process or use of the 'defined' unary operator does
168 // not match one of the two specified forms prior to macro replacement, the
169 // behavior is undefined.
170 // This isn't an idle threat, consider this program:
171 // #define FOO
172 // #define BAR defined(FOO)
173 // #if BAR
174 // ...
175 // #else
176 // ...
177 // #endif
178 // clang and gcc will pick the #if branch while Visual Studio will take the
179 // #else branch. Emit a warning about this undefined behavior.
180 if (beginLoc.isMacroID()) {
181 bool IsFunctionTypeMacro =
182 PP.getSourceManager()
183 .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
184 .getExpansion()
185 .isFunctionMacroExpansion();
186 // For object-type macros, it's easy to replace
187 // #define FOO defined(BAR)
188 // with
189 // #if defined(BAR)
190 // #define FOO 1
191 // #else
192 // #define FOO 0
193 // #endif
194 // and doing so makes sense since compilers handle this differently in
195 // practice (see example further up). But for function-type macros,
196 // there is no good way to write
197 // # define FOO(x) (defined(M_ ## x) && M_ ## x)
198 // in a different way, and compilers seem to agree on how to behave here.
199 // So warn by default on object-type macros, but only warn in -pedantic
200 // mode on function-type macros.
201 if (IsFunctionTypeMacro)
202 PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
203 else
204 PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
205 }
206
John Thompsoncda95fe2013-07-19 18:50:04 +0000207 // Invoke the 'defined' callback.
208 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
Richard Smith36bd40d2015-05-04 03:15:40 +0000209 Callbacks->Defined(macroToken, Macro,
John Thompsoncda95fe2013-07-19 18:50:04 +0000210 SourceRange(beginLoc, PeekTok.getLocation()));
211 }
212
John Thompsonb5353522009-10-30 13:49:06 +0000213 // Success, remember that we saw defined(X).
214 DT.State = DefinedTracker::DefinedMacro;
215 DT.TheMacro = II;
216 return false;
217}
218
Chris Lattner22eb9722006-06-18 05:43:12 +0000219/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
220/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000221/// parsing. This function also returns information about the form of the
222/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +0000223///
224/// If ValueLive is false, then this value is being evaluated in a context where
225/// the result is not used. As such, avoid diagnostics that relate to
226/// evaluation.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000227static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
228 bool ValueLive, Preprocessor &PP) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000229 DT.State = DefinedTracker::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000230
Richard Smith4d247e72016-04-16 00:07:09 +0000231 Result.setIdentifier(nullptr);
232
Douglas Gregorec00a262010-08-24 22:20:20 +0000233 if (PeekTok.is(tok::code_completion)) {
234 if (PP.getCodeCompletionHandler())
235 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000236 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000237 PP.LexNonComment(PeekTok);
Douglas Gregorec00a262010-08-24 22:20:20 +0000238 }
Mike Stump11289f42009-09-09 15:08:12 +0000239
Chris Lattner22eb9722006-06-18 05:43:12 +0000240 switch (PeekTok.getKind()) {
Olivier Goffart90f981b2017-07-14 09:23:40 +0000241 default:
242 // If this token's spelling is a pp-identifier, check to see if it is
243 // 'defined' or if it is a macro. Note that we check here because many
244 // keywords are pp-identifiers, so we can't check the kind.
245 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
246 // Handle "defined X" and "defined(X)".
247 if (II->isStr("defined"))
248 return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
249
250 if (!II->isCPlusPlusOperatorKeyword()) {
251 // If this identifier isn't 'defined' or one of the special
252 // preprocessor keywords and it wasn't macro expanded, it turns
253 // into a simple 0
254 if (ValueLive)
255 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
256 Result.Val = 0;
257 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
258 Result.setIdentifier(II);
259 Result.setRange(PeekTok.getLocation());
260 DT.IncludedUndefinedIds = true;
261 PP.LexNonComment(PeekTok);
262 return false;
263 }
264 }
Chris Lattnerf8f94542008-04-13 20:38:43 +0000265 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000266 return true;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000267 case tok::eod:
Chris Lattner22eb9722006-06-18 05:43:12 +0000268 case tok::r_paren:
269 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000270 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000271 return true;
272 case tok::numeric_constant: {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000273 SmallString<64> IntegerBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000274 bool NumberInvalid = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000275 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000276 &NumberInvalid);
277 if (NumberInvalid)
278 return true; // a diagnostic was already reported
279
Dmitri Gribenko7ba91722012-09-24 09:53:54 +0000280 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000281 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000282 return true; // a diagnostic was already reported.
Mike Stump11289f42009-09-09 15:08:12 +0000283
Chris Lattnered045422007-08-26 03:29:23 +0000284 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000285 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000286 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000287 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000288 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000289
Richard Smith39570d002012-03-08 08:45:32 +0000290 // Complain about, and drop, any ud-suffix.
291 if (Literal.hasUDSuffix())
292 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
293
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000294 // 'long long' is a C99 or C++11 feature.
295 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
296 if (PP.getLangOpts().CPlusPlus)
297 PP.Diag(PeekTok,
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000298 PP.getLangOpts().CPlusPlus11 ?
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000299 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
300 else
301 PP.Diag(PeekTok, diag::ext_c99_longlong);
302 }
Neil Boothac582c52007-08-29 22:00:19 +0000303
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000304 // Parse the integer literal into Result.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000305 if (Literal.GetIntegerValue(Result.Val)) {
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000306 // Overflow parsing integer literal.
Aaron Ballman446867e2014-07-22 14:08:09 +0000307 if (ValueLive)
Aaron Ballman31f42312014-07-24 14:51:23 +0000308 PP.Diag(PeekTok, diag::err_integer_literal_too_large)
309 << /* Unsigned */ 1;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000310 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000311 } else {
312 // Set the signedness of the result to match whether there was a U suffix
313 // or not.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000314 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump11289f42009-09-09 15:08:12 +0000315
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000316 // Detect overflow based on whether the value is signed. If signed
317 // and if the value is too large, emit a warning "integer constant is so
318 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
319 // is 64-bits.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000320 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Richard Smith7e34fbd2014-03-14 21:21:24 +0000321 // Octal, hexadecimal, and binary literals are implicitly unsigned if
322 // the value does not fit into a signed integer type.
Eli Friedman088d39a2013-07-23 00:25:18 +0000323 if (ValueLive && Literal.getRadix() == 10)
Aaron Ballman31f42312014-07-24 14:51:23 +0000324 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000325 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000326 }
327 }
Mike Stump11289f42009-09-09 15:08:12 +0000328
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000329 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000330 Result.setRange(PeekTok.getLocation());
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000331 PP.LexNonComment(PeekTok);
332 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000333 }
Douglas Gregorfb65e592011-07-27 05:40:30 +0000334 case tok::char_constant: // 'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000335 case tok::wide_char_constant: // L'x'
Richard Smith3e3a7052014-11-08 06:08:42 +0000336 case tok::utf8_char_constant: // u8'x'
Douglas Gregorfb65e592011-07-27 05:40:30 +0000337 case tok::utf16_char_constant: // u'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000338 case tok::utf32_char_constant: { // U'x'
Richard Smithd67aea22012-03-06 03:21:47 +0000339 // Complain about, and drop, any ud-suffix.
340 if (PeekTok.hasUDSuffix())
Richard Smith39570d002012-03-08 08:45:32 +0000341 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smithd67aea22012-03-06 03:21:47 +0000342
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000343 SmallString<32> CharBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000344 bool CharInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000345 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000346 if (CharInvalid)
347 return true;
Benjamin Kramer0a1abd42010-02-27 13:44:12 +0000348
349 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregorfb65e592011-07-27 05:40:30 +0000350 PeekTok.getLocation(), PP, PeekTok.getKind());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000351 if (Literal.hadError())
352 return true; // A diagnostic was already emitted.
353
354 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar1b444192009-11-13 05:51:54 +0000355 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedmand8cec572009-06-01 05:25:02 +0000356 unsigned NumBits;
357 if (Literal.isMultiChar())
358 NumBits = TI.getIntWidth();
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000359 else if (Literal.isWide())
360 NumBits = TI.getWCharWidth();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000361 else if (Literal.isUTF16())
362 NumBits = TI.getChar16Width();
363 else if (Literal.isUTF32())
364 NumBits = TI.getChar32Width();
Richard Smith3a8244d2018-05-01 05:02:45 +0000365 else // char or char8_t
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000366 NumBits = TI.getCharWidth();
Eli Friedmand8cec572009-06-01 05:25:02 +0000367
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000368 // Set the width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000369 llvm::APSInt Val(NumBits);
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000370 // Set the value.
371 Val = Literal.getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000372 // Set the signedness. UTF-16 and UTF-32 are always unsigned
Michael Wong867eeb52015-02-24 13:34:20 +0000373 if (Literal.isWide())
374 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
375 else if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikiebbafb8a2012-03-11 07:00:24 +0000376 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000377
Chris Lattner3565c8e2008-05-05 06:45:50 +0000378 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
379 Result.Val = Val.extend(Result.Val.getBitWidth());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000380 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000381 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000382 "intmax_t smaller than char/wchar_t?");
Chris Lattner3565c8e2008-05-05 06:45:50 +0000383 Result.Val = Val;
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000384 }
385
386 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000387 Result.setRange(PeekTok.getLocation());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000388 PP.LexNonComment(PeekTok);
389 return false;
390 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000391 case tok::l_paren: {
392 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000393 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000394 // Parse the value and if there are any binary operators involved, parse
395 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000396 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000397
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000398 // If this is a silly value like (X), which doesn't need parens, check for
399 // !(defined X).
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000400 if (PeekTok.is(tok::r_paren)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000401 // Just use DT unmodified as our result.
402 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000403 // Otherwise, we have something like (x+y), and we consumed '(x'.
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000404 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
405 DT.IncludedUndefinedIds, PP))
Chris Lattner86054a92007-04-10 05:26:38 +0000406 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000408 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000409 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
410 << Result.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000411 PP.Diag(Start, diag::note_matching) << tok::l_paren;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000412 return true;
413 }
414 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000415 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000416 Result.setRange(Start, PeekTok.getLocation());
Richard Smith4d247e72016-04-16 00:07:09 +0000417 Result.setIdentifier(nullptr);
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000418 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000419 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000420 }
421 case tok::plus: {
422 SourceLocation Start = PeekTok.getLocation();
Chris Lattner22eb9722006-06-18 05:43:12 +0000423 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000424 PP.LexNonComment(PeekTok);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000425 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
426 Result.setBegin(Start);
Richard Smith4d247e72016-04-16 00:07:09 +0000427 Result.setIdentifier(nullptr);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000428 return false;
429 }
Chris Lattner7e61ac52007-04-11 03:42:36 +0000430 case tok::minus: {
431 SourceLocation Loc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000432 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000433 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000434 Result.setBegin(Loc);
Richard Smith4d247e72016-04-16 00:07:09 +0000435 Result.setIdentifier(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000436
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000437 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000438 Result.Val = -Result.Val;
Mike Stump11289f42009-09-09 15:08:12 +0000439
Chris Lattner1cb0e612008-07-03 03:47:30 +0000440 // -MININT is the only thing that overflows. Unsigned never overflows.
441 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chris Lattner7e61ac52007-04-11 03:42:36 +0000443 // If this operator is live and overflowed, report the issue.
444 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000445 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000447 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000448 return false;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000449 }
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattner3565c8e2008-05-05 06:45:50 +0000451 case tok::tilde: {
452 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000453 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000454 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000455 Result.setBegin(Start);
Richard Smith4d247e72016-04-16 00:07:09 +0000456 Result.setIdentifier(nullptr);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000457
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000458 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000459 Result.Val = ~Result.Val;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000460 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000461 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattner3565c8e2008-05-05 06:45:50 +0000464 case tok::exclaim: {
465 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000466 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000467 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000468 Result.setBegin(Start);
469 Result.Val = !Result.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000470 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000471 Result.Val.setIsUnsigned(false);
Richard Smith4d247e72016-04-16 00:07:09 +0000472 Result.setIdentifier(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000473
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000474 if (DT.State == DefinedTracker::DefinedMacro)
475 DT.State = DefinedTracker::NotDefinedMacro;
476 else if (DT.State == DefinedTracker::NotDefinedMacro)
477 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000478 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000479 }
Olivier Goffart90f981b2017-07-14 09:23:40 +0000480 case tok::kw_true:
481 case tok::kw_false:
482 Result.Val = PeekTok.getKind() == tok::kw_true;
483 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
484 Result.setIdentifier(PeekTok.getIdentifierInfo());
485 Result.setRange(PeekTok.getLocation());
486 PP.LexNonComment(PeekTok);
487 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000488
Chris Lattner22eb9722006-06-18 05:43:12 +0000489 // FIXME: Handle #assert
490 }
491}
492
Chris Lattner22eb9722006-06-18 05:43:12 +0000493/// getPrecedence - Return the precedence of the specified binary operator
494/// token. This returns:
495/// ~0 - Invalid token.
Chris Lattner3c57f7e2008-05-05 04:10:51 +0000496/// 14 -> 3 - various operators.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000497/// 0 - 'eod' or ')'
Chris Lattner22eb9722006-06-18 05:43:12 +0000498static unsigned getPrecedence(tok::TokenKind Kind) {
499 switch (Kind) {
500 default: return ~0U;
501 case tok::percent:
502 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000503 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000504 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000505 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000506 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000507 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000508 case tok::lessequal:
509 case tok::less:
510 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000511 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000512 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000513 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000514 case tok::amp: return 9;
515 case tok::caret: return 8;
516 case tok::pipe: return 7;
517 case tok::ampamp: return 6;
518 case tok::pipepipe: return 5;
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000519 case tok::question: return 4;
520 case tok::comma: return 3;
Chris Lattnerd89e4582008-05-04 18:36:18 +0000521 case tok::colon: return 2;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000522 case tok::r_paren: return 0;// Lowest priority, end of expr.
523 case tok::eod: return 0;// Lowest priority, end of directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000524 }
525}
526
Richard Smith4d247e72016-04-16 00:07:09 +0000527static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
528 Token &Tok) {
529 if (Tok.is(tok::l_paren) && LHS.getIdentifier())
530 PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
531 << LHS.getIdentifier();
532 else
533 PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
534 << LHS.getRange();
535}
Chris Lattner22eb9722006-06-18 05:43:12 +0000536
537/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner3565c8e2008-05-05 06:45:50 +0000538/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Chris Lattner86054a92007-04-10 05:26:38 +0000539///
540/// If ValueLive is false, then this value is being evaluated in a context where
541/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner3565c8e2008-05-05 06:45:50 +0000542/// evaluation, such as division by zero warnings.
543static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +0000544 Token &PeekTok, bool ValueLive,
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000545 bool &IncludedUndefinedIds,
Chris Lattner86054a92007-04-10 05:26:38 +0000546 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000547 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
548 // If this token isn't valid, report the error.
549 if (PeekPrec == ~0U) {
Richard Smith4d247e72016-04-16 00:07:09 +0000550 diagnoseUnexpectedOperator(PP, LHS, PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000551 return true;
552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000554 while (true) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000555 // If this token has a lower precedence than we are allowed to parse, return
556 // it so that higher levels of the recursion can parse it.
557 if (PeekPrec < MinPrec)
558 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattner22eb9722006-06-18 05:43:12 +0000560 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump11289f42009-09-09 15:08:12 +0000561
Chris Lattner86054a92007-04-10 05:26:38 +0000562 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump11289f42009-09-09 15:08:12 +0000563 // dead. Note that this cannot just clobber ValueLive. Consider
Chris Lattner86054a92007-04-10 05:26:38 +0000564 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
565 // this example, the RHS of the && being dead does not make the rest of the
566 // expr dead.
567 bool RHSIsLive;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000568 if (Operator == tok::ampamp && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000569 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000570 else if (Operator == tok::pipepipe && LHS.Val != 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000571 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000572 else if (Operator == tok::question && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000573 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
574 else
575 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000576
Chris Lattner3565c8e2008-05-05 06:45:50 +0000577 // Consume the operator, remembering the operator's location for reporting.
578 SourceLocation OpLoc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000579 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000580
Chris Lattner3565c8e2008-05-05 06:45:50 +0000581 PPValue RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000582 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000583 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000584 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000585 IncludedUndefinedIds = DT.IncludedUndefinedIds;
Chris Lattner22eb9722006-06-18 05:43:12 +0000586
587 // Remember the precedence of this operator and get the precedence of the
588 // operator immediately to the right of the RHS.
589 unsigned ThisPrec = PeekPrec;
590 PeekPrec = getPrecedence(PeekTok.getKind());
591
592 // If this token isn't valid, report the error.
593 if (PeekPrec == ~0U) {
Richard Smith4d247e72016-04-16 00:07:09 +0000594 diagnoseUnexpectedOperator(PP, RHS, PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000595 return true;
596 }
Mike Stump11289f42009-09-09 15:08:12 +0000597
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000598 // Decide whether to include the next binop in this subexpression. For
599 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattnerca2b3182008-05-05 20:09:27 +0000600 // handle y*z as a single subexpression. We do this because the precedence
601 // of * is higher than that of +. The only strange case we have to handle
602 // here is for the ?: operator, where the precedence is actually lower than
603 // the LHS of the '?'. The grammar rule is:
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000604 //
605 // conditional-expression ::=
606 // logical-OR-expression ? expression : conditional-expression
607 // where 'expression' is actually comma-expression.
608 unsigned RHSPrec;
609 if (Operator == tok::question)
610 // The RHS of "?" should be maximally consumed as an expression.
611 RHSPrec = getPrecedence(tok::comma);
612 else // All others should munch while higher precedence.
613 RHSPrec = ThisPrec+1;
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000615 if (PeekPrec >= RHSPrec) {
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000616 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
617 IncludedUndefinedIds, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000618 return true;
619 PeekPrec = getPrecedence(PeekTok.getKind());
620 }
621 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump11289f42009-09-09 15:08:12 +0000622
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000623 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump11289f42009-09-09 15:08:12 +0000624 // either operand is unsigned.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000625 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattnerca671b02008-05-04 23:46:17 +0000626 switch (Operator) {
627 case tok::question: // No UAC for x and y in "x ? y : z".
628 case tok::lessless: // Shift amount doesn't UAC with shift value.
629 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
630 case tok::comma: // Comma operands are not subject to UACs.
631 case tok::pipepipe: // Logical || does not do UACs.
632 case tok::ampamp: // Logical && does not do UACs.
633 break; // No UAC
634 default:
Chris Lattner99ca0912007-04-11 04:14:45 +0000635 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
636 // If this just promoted something from signed to unsigned, and if the
637 // value was negative, warn about it.
638 if (ValueLive && Res.isUnsigned()) {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000639 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Craig Topper7f5ff212015-11-14 02:09:55 +0000640 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
Chris Lattnere05c4df2008-11-18 21:48:13 +0000641 << LHS.Val.toString(10, true) + " to " +
642 LHS.Val.toString(10, false)
643 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000644 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Craig Topper7f5ff212015-11-14 02:09:55 +0000645 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
Chris Lattnere05c4df2008-11-18 21:48:13 +0000646 << RHS.Val.toString(10, true) + " to " +
647 RHS.Val.toString(10, false)
648 << LHS.getRange() << RHS.getRange();
Chris Lattner99ca0912007-04-11 04:14:45 +0000649 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000650 LHS.Val.setIsUnsigned(Res.isUnsigned());
651 RHS.Val.setIsUnsigned(Res.isUnsigned());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000652 }
Mike Stump11289f42009-09-09 15:08:12 +0000653
Chris Lattner5a0f1642007-04-10 06:54:33 +0000654 bool Overflow = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000655 switch (Operator) {
David Blaikie83d382b2011-09-23 05:06:16 +0000656 default: llvm_unreachable("Unknown operator token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000657 case tok::percent:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000658 if (RHS.Val != 0)
659 Res = LHS.Val % RHS.Val;
660 else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000661 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
662 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000663 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000664 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000665 break;
666 case tok::slash:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000667 if (RHS.Val != 0) {
Chris Lattner2edb9262010-10-13 23:46:56 +0000668 if (LHS.Val.isSigned())
669 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
670 else
671 Res = LHS.Val / RHS.Val;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000672 } else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000673 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
674 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000675 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000676 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000677 break;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000679 case tok::star:
Chris Lattner2edb9262010-10-13 23:46:56 +0000680 if (Res.isSigned())
681 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
682 else
683 Res = LHS.Val * RHS.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000684 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000685 case tok::lessless: {
686 // Determine whether overflow is about to happen.
David Majnemer3e8b6ac2014-10-13 22:18:22 +0000687 if (LHS.isUnsigned())
688 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
689 else
690 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000691 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000692 }
693 case tok::greatergreater: {
694 // Determine whether overflow is about to happen.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000695 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Richard Trieucc3949d2016-02-18 22:34:54 +0000696 if (ShAmt >= LHS.getBitWidth()) {
697 Overflow = true;
698 ShAmt = LHS.getBitWidth()-1;
699 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000700 Res = LHS.Val >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000701 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000702 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000703 case tok::plus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000704 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000705 Res = LHS.Val + RHS.Val;
706 else
707 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000708 break;
709 case tok::minus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000710 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000711 Res = LHS.Val - RHS.Val;
712 else
713 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000714 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000715 case tok::lessequal:
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::less:
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::greaterequal:
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;
727 case tok::greater:
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.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000730 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000731 case tok::exclaimequal:
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;
735 case tok::equalequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000736 Res = LHS.Val == RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000737 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000738 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000739 case tok::amp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000740 Res = LHS.Val & RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000741 break;
742 case tok::caret:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000743 Res = LHS.Val ^ RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000744 break;
745 case tok::pipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000746 Res = LHS.Val | RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000747 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000748 case tok::ampamp:
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.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000751 break;
752 case tok::pipepipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000753 Res = (LHS.Val != 0 || RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000754 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000755 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000756 case tok::comma:
Chris Lattnerd89e4582008-05-04 18:36:18 +0000757 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
758 // if not being evaluated.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000759 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000760 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
761 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000762 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump11289f42009-09-09 15:08:12 +0000763 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000764 case tok::question: {
765 // Parse the : part of the expression.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000766 if (PeekTok.isNot(tok::colon)) {
Alp Toker35d87032013-12-30 23:29:50 +0000767 PP.Diag(PeekTok.getLocation(), diag::err_expected)
768 << tok::colon << LHS.getRange() << RHS.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000769 PP.Diag(OpLoc, diag::note_matching) << tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +0000770 return true;
771 }
772 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000773 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000774
775 // Evaluate the value after the :.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000776 bool AfterColonLive = ValueLive && LHS.Val == 0;
777 PPValue AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000778 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000779 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
780 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000781
Chris Lattnerca2b3182008-05-05 20:09:27 +0000782 // Parse anything after the : with the same precedence as ?. We allow
783 // things of equal precedence because ?: is right associative.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000784 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000785 PeekTok, AfterColonLive,
786 IncludedUndefinedIds, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000787 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000788
Chris Lattner22eb9722006-06-18 05:43:12 +0000789 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000790 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
791 RHS.setEnd(AfterColonVal.getRange().getEnd());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000792
793 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
794 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000795 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump11289f42009-09-09 15:08:12 +0000796
Chris Lattner22eb9722006-06-18 05:43:12 +0000797 // Figure out the precedence of the token after the : part.
798 PeekPrec = getPrecedence(PeekTok.getKind());
799 break;
800 }
801 case tok::colon:
802 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere05c4df2008-11-18 21:48:13 +0000803 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
804 << LHS.getRange() << RHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000805 return true;
806 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000807
808 // If this operator is live and overflowed, report the issue.
809 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000810 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
811 << LHS.getRange() << RHS.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000812
Chris Lattner9cc755d2007-04-10 07:07:11 +0000813 // Put the result back into 'LHS' for our next iteration.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000814 LHS.Val = Res;
815 LHS.setEnd(RHS.getRange().getEnd());
Richard Smith4d247e72016-04-16 00:07:09 +0000816 RHS.setIdentifier(nullptr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000817 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000818}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000819
820/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
821/// may occur after a #if or #elif directive. If the expression is equivalent
822/// to "!defined(X)" return X in IfNDefMacro.
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000823Preprocessor::DirectiveEvalResult
824Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
David Blaikie1dc4a3d2013-03-18 23:22:28 +0000825 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
Chris Lattner4c53c402009-12-14 05:00:18 +0000826 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
827 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
828 // in which case a directive is undefined behavior. We want macros to be able
829 // to recursively expand in order to get more gcc-list behavior, so we force
830 // DisableMacroExpansion to false and restore it when we're done parsing the
831 // expression.
832 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
833 DisableMacroExpansion = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000834
Chris Lattnere3519cc2006-07-04 18:11:39 +0000835 // Peek ahead one token.
Chris Lattner146762e2007-07-20 16:59:19 +0000836 Token Tok;
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000837 LexNonComment(Tok);
Fangrui Song6907ce22018-07-30 19:24:48 +0000838
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000839 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner37e05872008-03-05 18:54:05 +0000840 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattner3565c8e2008-05-05 06:45:50 +0000842 PPValue ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000843 DefinedTracker DT;
Aaron Ballman65e96bd2019-01-17 20:21:34 +0000844 SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
Chris Lattner86054a92007-04-10 05:26:38 +0000845 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000846 // Parse error, skip the rest of the macro line.
Aaron Ballman65e96bd2019-01-17 20:21:34 +0000847 SourceRange ConditionRange = ExprStartLoc;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000848 if (Tok.isNot(tok::eod))
Aaron Ballman65e96bd2019-01-17 20:21:34 +0000849 ConditionRange = DiscardUntilEndOfDirective();
Fangrui Song6907ce22018-07-30 19:24:48 +0000850
Chris Lattner4c53c402009-12-14 05:00:18 +0000851 // Restore 'DisableMacroExpansion'.
852 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Aaron Ballman65e96bd2019-01-17 20:21:34 +0000853
854 // We cannot trust the source range from the value because there was a
855 // parse error. Track the range manually -- the end of the directive is the
856 // end of the condition range.
857 return {false,
858 DT.IncludedUndefinedIds,
859 {ExprStartLoc, ConditionRange.getEnd()}};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000860 }
Mike Stump11289f42009-09-09 15:08:12 +0000861
Chris Lattnere3519cc2006-07-04 18:11:39 +0000862 // If we are at the end of the expression after just parsing a value, there
863 // must be no (unparenthesized) binary operators involved, so we can exit
864 // directly.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000865 if (Tok.is(tok::eod)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000866 // If the expression we parsed was of the form !defined(macro), return the
867 // macro in IfNDefMacro.
868 if (DT.State == DefinedTracker::NotDefinedMacro)
869 IfNDefMacro = DT.TheMacro;
Mike Stump11289f42009-09-09 15:08:12 +0000870
Chris Lattner4c53c402009-12-14 05:00:18 +0000871 // Restore 'DisableMacroExpansion'.
872 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Aaron Ballman65e96bd2019-01-17 20:21:34 +0000873 return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875
Chris Lattnere3519cc2006-07-04 18:11:39 +0000876 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
877 // operator and the stuff after it.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000878 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000879 Tok, true, DT.IncludedUndefinedIds, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000880 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000881 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000882 DiscardUntilEndOfDirective();
Fangrui Song6907ce22018-07-30 19:24:48 +0000883
Chris Lattner4c53c402009-12-14 05:00:18 +0000884 // Restore 'DisableMacroExpansion'.
885 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Aaron Ballman65e96bd2019-01-17 20:21:34 +0000886 return {false, DT.IncludedUndefinedIds, ResVal.getRange()};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000889 // If we aren't at the tok::eod token, something bad happened, like an extra
Chris Lattnere3519cc2006-07-04 18:11:39 +0000890 // ')' token.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000891 if (Tok.isNot(tok::eod)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000892 Diag(Tok, diag::err_pp_expected_eol);
893 DiscardUntilEndOfDirective();
894 }
Mike Stump11289f42009-09-09 15:08:12 +0000895
Chris Lattner4c53c402009-12-14 05:00:18 +0000896 // Restore 'DisableMacroExpansion'.
897 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Aaron Ballman65e96bd2019-01-17 20:21:34 +0000898 return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000899}