25 #include "llvm/ADT/APSInt.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SaveAndRestore.h"
28 using namespace clang;
40 PPValue(
unsigned BitWidth) : Val(BitWidth) {}
42 unsigned getBitWidth()
const {
return Val.getBitWidth(); }
43 bool isUnsigned()
const {
return Val.isUnsigned(); }
45 const SourceRange &getRange()
const {
return Range; }
49 Range.setBegin(B); Range.setEnd(E);
58 Token &PeekTok,
bool ValueLive,
85 Result.setBegin(beginLoc);
92 if (PeekTok.
is(tok::l_paren)) {
98 if (PeekTok.
is(tok::code_completion)) {
112 Result.Val = !!Macro;
113 Result.Val.setIsUnsigned(
false);
116 if (Result.Val != 0 && ValueLive)
120 Token macroToken(PeekTok);
128 if (PeekTok.
isNot(tok::r_paren)) {
130 <<
"'defined'" << tok::r_paren;
131 PP.
Diag(LParenLoc, diag::note_matching) << tok::l_paren;
145 Callbacks->Defined(macroToken, Macro,
167 if (PeekTok.
is(tok::code_completion)) {
179 if (II->isStr(
"defined"))
187 II->getTokenID() != tok::kw_true &&
188 II->getTokenID() != tok::kw_false)
189 PP.
Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
190 Result.Val = II->getTokenID() == tok::kw_true;
191 Result.Val.setIsUnsigned(
false);
199 PP.
Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
204 PP.
Diag(PeekTok, diag::err_pp_expected_value_in_expr);
206 case tok::numeric_constant: {
208 bool NumberInvalid =
false;
209 StringRef Spelling = PP.
getSpelling(PeekTok, IntegerBuffer,
215 if (Literal.hadError)
218 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
219 PP.
Diag(PeekTok, diag::err_pp_illegal_floating_literal);
222 assert(Literal.isIntegerLiteral() &&
"Unknown ppnumber");
225 if (Literal.hasUDSuffix())
226 PP.
Diag(PeekTok, diag::err_pp_invalid_udl) << 1;
233 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
235 PP.
Diag(PeekTok, diag::ext_c99_longlong);
239 if (Literal.GetIntegerValue(Result.Val)) {
242 PP.
Diag(PeekTok, diag::err_integer_literal_too_large)
244 Result.Val.setIsUnsigned(
true);
248 Result.Val.setIsUnsigned(Literal.isUnsigned);
254 if (!Literal.isUnsigned && Result.Val.isNegative()) {
257 if (ValueLive && Literal.getRadix() == 10)
258 PP.
Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
259 Result.Val.setIsUnsigned(
true);
268 case tok::char_constant:
269 case tok::wide_char_constant:
270 case tok::utf8_char_constant:
271 case tok::utf16_char_constant:
272 case tok::utf32_char_constant: {
275 PP.
Diag(PeekTok, diag::err_pp_invalid_udl) << 0;
278 bool CharInvalid =
false;
279 StringRef ThisTok = PP.
getSpelling(PeekTok, CharBuffer, &CharInvalid);
285 if (Literal.hadError())
291 if (Literal.isMultiChar())
293 else if (Literal.isWide())
295 else if (Literal.isUTF16())
297 else if (Literal.isUTF32())
303 llvm::APSInt Val(NumBits);
305 Val = Literal.getValue();
307 if (Literal.isWide())
309 else if (!Literal.isUTF16() && !Literal.isUTF32())
312 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
313 Result.Val = Val.extend(Result.Val.getBitWidth());
315 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
316 "intmax_t smaller than char/wchar_t?");
330 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
334 if (PeekTok.
is(tok::r_paren)) {
341 if (PeekTok.
isNot(tok::r_paren)) {
343 << Result.getRange();
344 PP.
Diag(Start, diag::note_matching) << tok::l_paren;
357 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
358 Result.setBegin(Start);
364 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
365 Result.setBegin(Loc);
368 Result.Val = -Result.Val;
371 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
374 if (Overflow && ValueLive)
375 PP.
Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
384 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
385 Result.setBegin(Start);
388 Result.Val = ~Result.Val;
396 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
397 Result.setBegin(Start);
398 Result.Val = !Result.Val;
400 Result.Val.setIsUnsigned(
false);
425 case tok::star:
return 14;
427 case tok::minus:
return 13;
429 case tok::greatergreater:
return 12;
432 case tok::greaterequal:
433 case tok::greater:
return 11;
434 case tok::exclaimequal:
435 case tok::equalequal:
return 10;
436 case tok::amp:
return 9;
437 case tok::caret:
return 8;
438 case tok::pipe:
return 7;
439 case tok::ampamp:
return 6;
440 case tok::pipepipe:
return 5;
441 case tok::question:
return 4;
442 case tok::comma:
return 3;
443 case tok::colon:
return 2;
444 case tok::r_paren:
return 0;
445 case tok::eod:
return 0;
457 Token &PeekTok,
bool ValueLive,
461 if (PeekPrec == ~0U) {
470 if (PeekPrec < MinPrec)
481 if (Operator == tok::ampamp && LHS.Val == 0)
483 else if (Operator == tok::pipepipe && LHS.Val != 0)
485 else if (Operator == tok::question && LHS.Val == 0)
488 RHSIsLive = ValueLive;
494 PPValue RHS(LHS.getBitWidth());
497 if (
EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP))
return true;
501 unsigned ThisPrec = PeekPrec;
505 if (PeekPrec == ~0U) {
522 if (Operator == tok::question)
526 RHSPrec = ThisPrec+1;
528 if (PeekPrec >= RHSPrec) {
533 assert(PeekPrec <= ThisPrec &&
"Recursion didn't work!");
537 llvm::APSInt Res(LHS.getBitWidth());
541 case tok::greatergreater:
547 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
550 if (ValueLive && Res.isUnsigned()) {
551 if (!LHS.isUnsigned() && LHS.Val.isNegative())
552 PP.
Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
553 << LHS.Val.toString(10,
true) +
" to " +
554 LHS.Val.toString(10,
false)
555 << LHS.getRange() << RHS.getRange();
556 if (!RHS.isUnsigned() && RHS.Val.isNegative())
557 PP.
Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
558 << RHS.Val.toString(10,
true) +
" to " +
559 RHS.Val.toString(10,
false)
560 << LHS.getRange() << RHS.getRange();
562 LHS.Val.setIsUnsigned(Res.isUnsigned());
563 RHS.Val.setIsUnsigned(Res.isUnsigned());
566 bool Overflow =
false;
568 default: llvm_unreachable(
"Unknown operator token!");
571 Res = LHS.Val % RHS.Val;
572 else if (ValueLive) {
573 PP.
Diag(OpLoc, diag::err_pp_remainder_by_zero)
574 << LHS.getRange() << RHS.getRange();
580 if (LHS.Val.isSigned())
581 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow),
false);
583 Res = LHS.Val / RHS.Val;
584 }
else if (ValueLive) {
585 PP.
Diag(OpLoc, diag::err_pp_division_by_zero)
586 << LHS.getRange() << RHS.getRange();
593 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow),
false);
595 Res = LHS.Val * RHS.Val;
597 case tok::lessless: {
599 if (LHS.isUnsigned())
600 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
602 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow),
false);
605 case tok::greatergreater: {
607 unsigned ShAmt =
static_cast<unsigned>(RHS.Val.getLimitedValue());
608 if (ShAmt >= LHS.getBitWidth())
609 Overflow =
true, ShAmt = LHS.getBitWidth()-1;
610 Res = LHS.Val >> ShAmt;
614 if (LHS.isUnsigned())
615 Res = LHS.Val + RHS.Val;
617 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow),
false);
620 if (LHS.isUnsigned())
621 Res = LHS.Val - RHS.Val;
623 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow),
false);
626 Res = LHS.Val <= RHS.Val;
627 Res.setIsUnsigned(
false);
630 Res = LHS.Val < RHS.Val;
631 Res.setIsUnsigned(
false);
633 case tok::greaterequal:
634 Res = LHS.Val >= RHS.Val;
635 Res.setIsUnsigned(
false);
638 Res = LHS.Val > RHS.Val;
639 Res.setIsUnsigned(
false);
641 case tok::exclaimequal:
642 Res = LHS.Val != RHS.Val;
643 Res.setIsUnsigned(
false);
645 case tok::equalequal:
646 Res = LHS.Val == RHS.Val;
647 Res.setIsUnsigned(
false);
650 Res = LHS.Val & RHS.Val;
653 Res = LHS.Val ^ RHS.Val;
656 Res = LHS.Val | RHS.Val;
659 Res = (LHS.Val != 0 && RHS.Val != 0);
660 Res.setIsUnsigned(
false);
663 Res = (LHS.Val != 0 || RHS.Val != 0);
664 Res.setIsUnsigned(
false);
670 PP.
Diag(OpLoc, diag::ext_pp_comma_expr)
671 << LHS.getRange() << RHS.getRange();
674 case tok::question: {
676 if (PeekTok.
isNot(tok::colon)) {
678 << tok::colon << LHS.getRange() << RHS.getRange();
679 PP.
Diag(OpLoc, diag::note_matching) << tok::question;
686 bool AfterColonLive = ValueLive && LHS.Val == 0;
687 PPValue AfterColonVal(LHS.getBitWidth());
689 if (
EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
695 PeekTok, AfterColonLive, PP))
699 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
700 RHS.setEnd(AfterColonVal.getRange().getEnd());
704 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
712 PP.
Diag(OpLoc, diag::err_pp_colon_without_question)
713 << LHS.getRange() << RHS.getRange();
718 if (Overflow && ValueLive)
719 PP.
Diag(OpLoc, diag::warn_pp_expr_overflow)
720 << LHS.getRange() << RHS.getRange();
724 LHS.setEnd(RHS.getRange().getEnd());
731 bool Preprocessor::EvaluateDirectiveExpression(
IdentifierInfo *&IfNDefMacro) {
739 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
740 DisableMacroExpansion =
false;
749 PPValue ResVal(BitWidth);
753 if (Tok.
isNot(tok::eod))
757 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
764 if (Tok.
is(tok::eod)) {
771 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
772 return ResVal.Val != 0;
780 if (Tok.
isNot(tok::eod))
784 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
790 if (Tok.
isNot(tok::eod)) {
791 Diag(Tok, diag::err_pp_expected_eol);
796 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
797 return ResVal.Val != 0;
void markMacroAsUsed(MacroInfo *MI)
A macro is used, update information about macros that need unused warnings.
unsigned getChar16Width() const
static unsigned getPrecedence(tok::TokenKind Kind)
Defines the clang::MacroInfo and clang::MacroDirective classes.
A description of the current definition of a macro.
void setCodeCompletionReached()
Note that we hit the code-completion point.
unsigned getChar32Width() const
CodeCompletionHandler * getCodeCompletionHandler() const
Retrieve the current code-completion handler.
void setBegin(SourceLocation b)
This interface provides a way to observe the actions of the preprocessor as it does its thing...
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, bool *ShadowFlag=nullptr)
const LangOptions & getLangOpts() const
MacroInfo * getMacroInfo() const
Get the MacroInfo that should be used for this definition.
static bool isTypeSigned(IntType T)
Returns true if the type is signed; false otherwise.
void LexNonComment(Token &Result)
Lex a token. If it's a comment, keep lexing until we get something not a comment. ...
tok::TokenKind getKind() const
const TargetInfo & getTargetInfo() const
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
unsigned getWCharWidth() const
Exposes information about the current target.
Defines the clang::Preprocessor interface.
bool hasUDSuffix() const
Return true if this token is a string or character literal which has a ud-suffix. ...
unsigned getIntMaxTWidth() const
Return the size of intmax_t and uintmax_t for this target, in bits.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT, bool ValueLive, Preprocessor &PP)
bool isNot(tok::TokenKind K) const
IdentifierInfo * TheMacro
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
bool isValid() const
Return true if this is a valid SourceLocation object.
MacroDefinition getMacroDefinition(const IdentifierInfo *II)
enum DefinedTracker::TrackerState State
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
PPCallbacks * getPPCallbacks() const
Accessors for preprocessor callbacks.
bool is(tok::TokenKind K) const
IntType getWCharType() const
static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT, bool ValueLive, Preprocessor &PP)
EvaluateDefined - Process a 'defined(sym)' expression.
virtual void CodeCompletePreprocessorExpression()
Callback invoked when performing code completion in a preprocessor expression, such as the condition ...
unsigned getCharWidth() const
unsigned getIntWidth() const
void LexUnexpandedNonComment(Token &Result)
Like LexNonComment, but this disables macro expansion of identifier tokens.
virtual void CodeCompleteMacroName(bool IsDefinition)
Callback invoked when performing code completion in a context where the name of a macro is expected...
static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec, Token &PeekTok, bool ValueLive, Preprocessor &PP)
Defines the clang::TargetInfo interface.
void DiscardUntilEndOfDirective()
Read and discard all tokens remaining on the current line until the tok::eod token is found...
A trivial tuple used to represent a source range.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
IdentifierInfo * getIdentifierInfo() const