25 #include "llvm/ADT/APSInt.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SaveAndRestore.h"
28 using namespace clang;
41 PPValue(
unsigned BitWidth) : Val(BitWidth) {}
48 unsigned getBitWidth()
const {
return Val.getBitWidth(); }
49 bool isUnsigned()
const {
return Val.isUnsigned(); }
55 Range.setBegin(B); Range.setEnd(E);
64 Token &PeekTok,
bool ValueLive,
91 Result.setBegin(beginLoc);
98 if (PeekTok.
is(tok::l_paren)) {
104 if (PeekTok.
is(tok::code_completion)) {
118 Result.Val = !!Macro;
119 Result.Val.setIsUnsigned(
false);
122 if (Result.Val != 0 && ValueLive)
126 Token macroToken(PeekTok);
134 if (PeekTok.
isNot(tok::r_paren)) {
136 <<
"'defined'" << tok::r_paren;
137 PP.
Diag(LParenLoc, diag::note_matching) << tok::l_paren;
167 if (beginLoc.isMacroID()) {
168 bool IsFunctionTypeMacro =
172 .isFunctionMacroExpansion();
188 if (IsFunctionTypeMacro)
189 PP.
Diag(beginLoc, diag::warn_defined_in_function_type_macro);
191 PP.
Diag(beginLoc, diag::warn_defined_in_object_type_macro);
196 Callbacks->Defined(macroToken, Macro,
218 Result.setIdentifier(
nullptr);
220 if (PeekTok.
is(tok::code_completion)) {
232 if (II->isStr(
"defined"))
240 II->getTokenID() != tok::kw_true &&
241 II->getTokenID() != tok::kw_false)
242 PP.
Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
243 Result.Val = II->getTokenID() == tok::kw_true;
244 Result.Val.setIsUnsigned(
false);
245 Result.setIdentifier(II);
253 PP.
Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
258 PP.
Diag(PeekTok, diag::err_pp_expected_value_in_expr);
260 case tok::numeric_constant: {
262 bool NumberInvalid =
false;
263 StringRef Spelling = PP.
getSpelling(PeekTok, IntegerBuffer,
269 if (Literal.hadError)
272 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
273 PP.
Diag(PeekTok, diag::err_pp_illegal_floating_literal);
276 assert(Literal.isIntegerLiteral() &&
"Unknown ppnumber");
279 if (Literal.hasUDSuffix())
280 PP.
Diag(PeekTok, diag::err_pp_invalid_udl) << 1;
287 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
289 PP.
Diag(PeekTok, diag::ext_c99_longlong);
293 if (Literal.GetIntegerValue(Result.Val)) {
296 PP.
Diag(PeekTok, diag::err_integer_literal_too_large)
298 Result.Val.setIsUnsigned(
true);
302 Result.Val.setIsUnsigned(Literal.isUnsigned);
308 if (!Literal.isUnsigned && Result.Val.isNegative()) {
311 if (ValueLive && Literal.getRadix() == 10)
312 PP.
Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
313 Result.Val.setIsUnsigned(
true);
322 case tok::char_constant:
323 case tok::wide_char_constant:
324 case tok::utf8_char_constant:
325 case tok::utf16_char_constant:
326 case tok::utf32_char_constant: {
329 PP.
Diag(PeekTok, diag::err_pp_invalid_udl) << 0;
332 bool CharInvalid =
false;
333 StringRef ThisTok = PP.
getSpelling(PeekTok, CharBuffer, &CharInvalid);
339 if (Literal.hadError())
345 if (Literal.isMultiChar())
347 else if (Literal.isWide())
349 else if (Literal.isUTF16())
351 else if (Literal.isUTF32())
357 llvm::APSInt Val(NumBits);
359 Val = Literal.getValue();
361 if (Literal.isWide())
363 else if (!Literal.isUTF16() && !Literal.isUTF32())
366 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
367 Result.Val = Val.extend(Result.Val.getBitWidth());
369 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
370 "intmax_t smaller than char/wchar_t?");
384 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
388 if (PeekTok.
is(tok::r_paren)) {
395 if (PeekTok.
isNot(tok::r_paren)) {
397 << Result.getRange();
398 PP.
Diag(Start, diag::note_matching) << tok::l_paren;
404 Result.setIdentifier(
nullptr);
412 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
413 Result.setBegin(Start);
414 Result.setIdentifier(
nullptr);
420 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
421 Result.setBegin(Loc);
422 Result.setIdentifier(
nullptr);
425 Result.Val = -Result.Val;
428 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
431 if (Overflow && ValueLive)
432 PP.
Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
441 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
442 Result.setBegin(Start);
443 Result.setIdentifier(
nullptr);
446 Result.Val = ~Result.Val;
454 if (
EvaluateValue(Result, PeekTok, DT, ValueLive, PP))
return true;
455 Result.setBegin(Start);
456 Result.Val = !Result.Val;
458 Result.Val.setIsUnsigned(
false);
459 Result.setIdentifier(
nullptr);
484 case tok::star:
return 14;
486 case tok::minus:
return 13;
488 case tok::greatergreater:
return 12;
491 case tok::greaterequal:
492 case tok::greater:
return 11;
493 case tok::exclaimequal:
494 case tok::equalequal:
return 10;
495 case tok::amp:
return 9;
496 case tok::caret:
return 8;
497 case tok::pipe:
return 7;
498 case tok::ampamp:
return 6;
499 case tok::pipepipe:
return 5;
500 case tok::question:
return 4;
501 case tok::comma:
return 3;
502 case tok::colon:
return 2;
503 case tok::r_paren:
return 0;
504 case tok::eod:
return 0;
510 if (Tok.
is(tok::l_paren) && LHS.getIdentifier())
511 PP.
Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
512 << LHS.getIdentifier();
525 Token &PeekTok,
bool ValueLive,
529 if (PeekPrec == ~0U) {
537 if (PeekPrec < MinPrec)
548 if (Operator == tok::ampamp && LHS.Val == 0)
550 else if (Operator == tok::pipepipe && LHS.Val != 0)
552 else if (Operator == tok::question && LHS.Val == 0)
555 RHSIsLive = ValueLive;
561 PPValue RHS(LHS.getBitWidth());
564 if (
EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP))
return true;
568 unsigned ThisPrec = PeekPrec;
572 if (PeekPrec == ~0U) {
588 if (Operator == tok::question)
592 RHSPrec = ThisPrec+1;
594 if (PeekPrec >= RHSPrec) {
599 assert(PeekPrec <= ThisPrec &&
"Recursion didn't work!");
603 llvm::APSInt Res(LHS.getBitWidth());
607 case tok::greatergreater:
613 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
616 if (ValueLive && Res.isUnsigned()) {
617 if (!LHS.isUnsigned() && LHS.Val.isNegative())
618 PP.
Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
619 << LHS.Val.toString(10,
true) +
" to " +
620 LHS.Val.toString(10,
false)
621 << LHS.getRange() << RHS.getRange();
622 if (!RHS.isUnsigned() && RHS.Val.isNegative())
623 PP.
Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
624 << RHS.Val.toString(10,
true) +
" to " +
625 RHS.Val.toString(10,
false)
626 << LHS.getRange() << RHS.getRange();
628 LHS.Val.setIsUnsigned(Res.isUnsigned());
629 RHS.Val.setIsUnsigned(Res.isUnsigned());
632 bool Overflow =
false;
634 default: llvm_unreachable(
"Unknown operator token!");
637 Res = LHS.Val % RHS.Val;
638 else if (ValueLive) {
639 PP.
Diag(OpLoc, diag::err_pp_remainder_by_zero)
640 << LHS.getRange() << RHS.getRange();
646 if (LHS.Val.isSigned())
647 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow),
false);
649 Res = LHS.Val / RHS.Val;
650 }
else if (ValueLive) {
651 PP.
Diag(OpLoc, diag::err_pp_division_by_zero)
652 << LHS.getRange() << RHS.getRange();
659 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow),
false);
661 Res = LHS.Val * RHS.Val;
663 case tok::lessless: {
665 if (LHS.isUnsigned())
666 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
668 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow),
false);
671 case tok::greatergreater: {
673 unsigned ShAmt =
static_cast<unsigned>(RHS.Val.getLimitedValue());
674 if (ShAmt >= LHS.getBitWidth()) {
676 ShAmt = LHS.getBitWidth()-1;
678 Res = LHS.Val >> ShAmt;
682 if (LHS.isUnsigned())
683 Res = LHS.Val + RHS.Val;
685 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow),
false);
688 if (LHS.isUnsigned())
689 Res = LHS.Val - RHS.Val;
691 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow),
false);
694 Res = LHS.Val <= RHS.Val;
695 Res.setIsUnsigned(
false);
698 Res = LHS.Val < RHS.Val;
699 Res.setIsUnsigned(
false);
701 case tok::greaterequal:
702 Res = LHS.Val >= RHS.Val;
703 Res.setIsUnsigned(
false);
706 Res = LHS.Val > RHS.Val;
707 Res.setIsUnsigned(
false);
709 case tok::exclaimequal:
710 Res = LHS.Val != RHS.Val;
711 Res.setIsUnsigned(
false);
713 case tok::equalequal:
714 Res = LHS.Val == RHS.Val;
715 Res.setIsUnsigned(
false);
718 Res = LHS.Val & RHS.Val;
721 Res = LHS.Val ^ RHS.Val;
724 Res = LHS.Val | RHS.Val;
727 Res = (LHS.Val != 0 && RHS.Val != 0);
728 Res.setIsUnsigned(
false);
731 Res = (LHS.Val != 0 || RHS.Val != 0);
732 Res.setIsUnsigned(
false);
738 PP.
Diag(OpLoc, diag::ext_pp_comma_expr)
739 << LHS.getRange() << RHS.getRange();
742 case tok::question: {
744 if (PeekTok.
isNot(tok::colon)) {
746 << tok::colon << LHS.getRange() << RHS.getRange();
747 PP.
Diag(OpLoc, diag::note_matching) << tok::question;
754 bool AfterColonLive = ValueLive && LHS.Val == 0;
755 PPValue AfterColonVal(LHS.getBitWidth());
757 if (
EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
763 PeekTok, AfterColonLive, PP))
767 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
768 RHS.setEnd(AfterColonVal.getRange().getEnd());
772 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
780 PP.
Diag(OpLoc, diag::err_pp_colon_without_question)
781 << LHS.getRange() << RHS.getRange();
786 if (Overflow && ValueLive)
787 PP.
Diag(OpLoc, diag::warn_pp_expr_overflow)
788 << LHS.getRange() << RHS.getRange();
792 LHS.setEnd(RHS.getRange().getEnd());
793 RHS.setIdentifier(
nullptr);
800 bool Preprocessor::EvaluateDirectiveExpression(
IdentifierInfo *&IfNDefMacro) {
808 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
809 DisableMacroExpansion =
false;
818 PPValue ResVal(BitWidth);
822 if (Tok.
isNot(tok::eod))
826 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
833 if (Tok.
is(tok::eod)) {
840 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
841 return ResVal.Val != 0;
849 if (Tok.
isNot(tok::eod))
853 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
859 if (Tok.
isNot(tok::eod)) {
860 Diag(Tok, diag::err_pp_expected_eol);
865 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
866 return ResVal.Val != 0;
SourceManager & getSourceManager() const
void markMacroAsUsed(MacroInfo *MI)
A macro is used, update information about macros that need unused warnings.
unsigned getChar16Width() const
getChar16Width/Align - Return the size of 'char16_t' for this target, in bits.
static unsigned getPrecedence(tok::TokenKind Kind)
getPrecedence - Return the precedence of the specified binary operator token.
const SrcMgr::SLocEntry & getSLocEntry(FileID FID, bool *Invalid=nullptr) const
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
getChar32Width/Align - Return the size of 'char32_t' for this target, in bits.
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
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
One of these records is kept for each identifier that is lexed.
bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, bool *ShadowFlag=nullptr)
const LangOptions & getLangOpts() const
Token - This structure provides full information about a lexed token.
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.
tok::TokenKind getKind() const
const TargetInfo & getTargetInfo() const
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
unsigned getWCharWidth() const
getWCharWidth/Align - Return the size of 'wchar_t' for this target, in bits.
static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS, Token &Tok)
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
Exposes information about the current target.
TrackerState
Each time a Value is evaluated, it returns information about whether the parsed value is of the form ...
Defines the clang::Preprocessor interface.
CharLiteralParser - Perform interpretation and semantic analysis of a character literal.
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)
EvaluateValue - Evaluate the token PeekTok (and any others needed) and return the computed value in R...
bool isNot(tok::TokenKind K) const
IdentifierInfo * TheMacro
TheMacro - When the state is DefinedMacro or NotDefinedMacro, this indicates the macro that was check...
Encodes a location in the source.
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.
const IdentifierInfo * getIdentifier() const
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
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 ...
DefinedTracker - This struct is used while parsing expressions to keep track of whether !defined(X) h...
unsigned getCharWidth() const
detail::InMemoryDirectory::const_iterator E
unsigned getIntWidth() const
getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for this target, in bits.
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)
EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is PeekTok, and whose precedence is PeekPrec.
Defines the clang::TargetInfo interface.
NumericLiteralParser - This performs strict semantic analysis of the content of a ppnumber...
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