Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ static unsigned getMaxCalculationWidth(const ASTContext &Context,
E = E->IgnoreParenImpCasts();

if (const auto *Bop = dyn_cast<BinaryOperator>(E)) {
unsigned LHSWidth = getMaxCalculationWidth(Context, Bop->getLHS());
unsigned RHSWidth = getMaxCalculationWidth(Context, Bop->getRHS());
const unsigned LHSWidth = getMaxCalculationWidth(Context, Bop->getLHS());
const unsigned RHSWidth = getMaxCalculationWidth(Context, Bop->getRHS());
if (Bop->getOpcode() == BO_Mul)
return LHSWidth + RHSWidth;
if (Bop->getOpcode() == BO_Add)
Expand All @@ -79,7 +79,7 @@ static unsigned getMaxCalculationWidth(const ASTContext &Context,
if (Uop->getOpcode() == UO_Not)
return 1024U;

QualType T = Uop->getType();
const QualType T = Uop->getType();
return T->isIntegerType() ? Context.getIntWidth(T) : 1024U;
} else if (const auto *I = dyn_cast<IntegerLiteral>(E)) {
return I->getValue().getActiveBits();
Expand Down Expand Up @@ -190,10 +190,10 @@ void MisplacedWideningCastCheck::check(const MatchFinder::MatchResult &Result) {
Calc->isTypeDependent() || Calc->isValueDependent())
return;

ASTContext &Context = *Result.Context;
const ASTContext &Context = *Result.Context;

QualType CastType = Cast->getType();
QualType CalcType = Calc->getType();
const QualType CastType = Cast->getType();
const QualType CalcType = Calc->getType();

// Explicit truncation using cast.
if (Context.getIntWidth(CastType) < Context.getIntWidth(CalcType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee,
const SourceManager &SM = Context.getSourceManager();
const LangOptions &LangOpts = Context.getLangOpts();

CharSourceRange CallRange =
const CharSourceRange CallRange =
Lexer::makeFileCharRange(CharSourceRange::getTokenRange(
Callee->getBeginLoc(), Callee->getEndLoc()),
SM, LangOpts);
Expand All @@ -39,7 +39,7 @@ static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee,
// std::move(). This will hopefully prevent erroneous replacements if the
// code does unusual things (e.g. create an alias for std::move() in
// another namespace).
NestedNameSpecifier NNS = Callee->getQualifier();
const NestedNameSpecifier NNS = Callee->getQualifier();
switch (NNS.getKind()) {
case NestedNameSpecifier::Kind::Null:
// Called as "move" (i.e. presumably the code had a "using std::move;").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ MultiLevelImplicitPointerConversionCheck::getCheckTraversalKind() const {
void MultiLevelImplicitPointerConversionCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *MatchedExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>("expr");
QualType Target = MatchedExpr->getType().getDesugaredType(*Result.Context);
QualType Source =
const QualType Target =
MatchedExpr->getType().getDesugaredType(*Result.Context);
const QualType Source =
MatchedExpr->getSubExpr()->getType().getDesugaredType(*Result.Context);

diag(MatchedExpr->getExprLoc(), "multilevel pointer conversion from %0 to "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ namespace {

AST_MATCHER_P(CXXTryStmt, hasHandlerFor,
ast_matchers::internal::Matcher<QualType>, InnerMatcher) {
for (unsigned NH = Node.getNumHandlers(), I = 0; I < NH; ++I) {
const unsigned NH = Node.getNumHandlers();
for (unsigned I = 0; I < NH; ++I) {
const CXXCatchStmt *CatchS = Node.getHandler(I);
// Check for generic catch handler (match anything).
if (CatchS->getCaughtType().isNull())
Expand All @@ -66,7 +67,7 @@ AST_MATCHER_P(CXXTryStmt, hasHandlerFor,
}

AST_MATCHER(CXXNewExpr, mayThrow) {
FunctionDecl *OperatorNew = Node.getOperatorNew();
const FunctionDecl *OperatorNew = Node.getOperatorNew();
if (!OperatorNew)
return false;
return !OperatorNew->getType()->castAs<FunctionProtoType>()->isNothrow();
Expand Down
40 changes: 20 additions & 20 deletions clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ AST_MATCHER_P(QualType, hasAnyType, std::vector<StringRef>, Names) {
if (Names.empty())
return false;

std::string Name = Node.getLocalUnqualifiedType().getAsString();
const std::string Name = Node.getLocalUnqualifiedType().getAsString();
return llvm::is_contained(Names, Name);
}

AST_MATCHER(FieldDecl, hasIntBitwidth) {
assert(Node.isBitField());
const ASTContext &Ctx = Node.getASTContext();
unsigned IntBitWidth = Ctx.getIntWidth(Ctx.IntTy);
unsigned CurrentBitWidth = Node.getBitWidthValue();
const unsigned IntBitWidth = Ctx.getIntWidth(Ctx.IntTy);
const unsigned CurrentBitWidth = Node.getBitWidthValue();
return IntBitWidth == CurrentBitWidth;
}

Expand Down Expand Up @@ -79,7 +79,7 @@ void NarrowingConversionsCheck::registerMatchers(MatchFinder *Finder) {
const auto IsCeilFloorCallExpr = expr(callExpr(callee(functionDecl(
hasAnyName("::ceil", "::std::ceil", "::floor", "::std::floor")))));

std::vector<StringRef> IgnoreConversionFromTypesVec =
const std::vector<StringRef> IgnoreConversionFromTypesVec =
utils::options::parseStringList(IgnoreConversionFromTypes);

// We may want to exclude other types from the checks, such as `size_type`
Expand Down Expand Up @@ -243,7 +243,7 @@ struct IntegerRange {
static IntegerRange createFromType(const ASTContext &Context,
const BuiltinType &T) {
if (T.isFloatingPoint()) {
unsigned PrecisionBits = llvm::APFloatBase::semanticsPrecision(
const unsigned PrecisionBits = llvm::APFloatBase::semanticsPrecision(
Context.getFloatTypeSemantics(T.desugar()));
// Contrary to two's complement integer, floating point values are
// symmetric and have the same number of positive and negative values.
Expand All @@ -262,24 +262,24 @@ static IntegerRange createFromType(const ASTContext &Context,
return {LowerValue, UpperValue};
}
assert(T.isInteger() && "Unexpected builtin type");
uint64_t TypeSize = Context.getTypeSize(&T);
bool IsUnsignedInteger = T.isUnsignedInteger();
const uint64_t TypeSize = Context.getTypeSize(&T);
const bool IsUnsignedInteger = T.isUnsignedInteger();
return {llvm::APSInt::getMinValue(TypeSize, IsUnsignedInteger),
llvm::APSInt::getMaxValue(TypeSize, IsUnsignedInteger)};
}

static bool isWideEnoughToHold(const ASTContext &Context,
const BuiltinType &FromType,
const BuiltinType &ToType) {
IntegerRange FromIntegerRange = createFromType(Context, FromType);
IntegerRange ToIntegerRange = createFromType(Context, ToType);
const IntegerRange FromIntegerRange = createFromType(Context, FromType);
const IntegerRange ToIntegerRange = createFromType(Context, ToType);
return ToIntegerRange.contains(FromIntegerRange);
}

static bool isWideEnoughToHold(const ASTContext &Context,
const llvm::APSInt &IntegerConstant,
const BuiltinType &ToType) {
IntegerRange ToIntegerRange = createFromType(Context, ToType);
const IntegerRange ToIntegerRange = createFromType(Context, ToType);
return ToIntegerRange.contains(IntegerConstant);
}

Expand All @@ -289,13 +289,13 @@ static bool isWideEnoughToHold(const ASTContext &Context,
static bool isFloatExactlyRepresentable(const ASTContext &Context,
const llvm::APFloat &FloatConstant,
const QualType &DestType) {
unsigned DestWidth = Context.getIntWidth(DestType);
bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
const unsigned DestWidth = Context.getIntWidth(DestType);
const bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
llvm::APSInt Result = llvm::APSInt(DestWidth, !DestSigned);
bool IsExact = false;
bool Overflows = FloatConstant.convertToInteger(
Result, llvm::APFloat::rmTowardZero, &IsExact) &
llvm::APFloat::opInvalidOp;
const bool Overflows = FloatConstant.convertToInteger(
Result, llvm::APFloat::rmTowardZero, &IsExact) &
llvm::APFloat::opInvalidOp;
return !Overflows && IsExact;
}

Expand All @@ -321,8 +321,8 @@ bool NarrowingConversionsCheck::isWarningInhibitedByEquivalentSize(
// With this option, we don't warn on conversions that have equivalent width
// in bits. eg. uint32 <-> int32.
if (!WarnOnEquivalentBitWidth) {
uint64_t FromTypeSize = Context.getTypeSize(&FromType);
uint64_t ToTypeSize = Context.getTypeSize(&ToType);
const uint64_t FromTypeSize = Context.getTypeSize(&FromType);
const uint64_t ToTypeSize = Context.getTypeSize(&ToType);
if (FromTypeSize == ToTypeSize) {
return true;
}
Expand Down Expand Up @@ -406,8 +406,8 @@ void NarrowingConversionsCheck::handleIntegralCast(const ASTContext &Context,
// With this option, we don't warn on conversions that have equivalent width
// in bits. eg. uint32 <-> int32.
if (!WarnOnEquivalentBitWidth) {
uint64_t FromTypeSize = Context.getTypeSize(FromType);
uint64_t ToTypeSize = Context.getTypeSize(ToType);
const uint64_t FromTypeSize = Context.getTypeSize(FromType);
const uint64_t ToTypeSize = Context.getTypeSize(ToType);
if (FromTypeSize == ToTypeSize)
return;
}
Expand Down Expand Up @@ -583,7 +583,7 @@ void NarrowingConversionsCheck::handleImplicitCast(
return;
if (handleConditionalOperator(Context, Lhs, Rhs))
return;
SourceLocation SourceLoc = Lhs.getExprLoc();
const SourceLocation SourceLoc = Lhs.getExprLoc();
switch (Cast.getCastKind()) {
case CK_BooleanToSignedIntegral:
handleBooleanToSignedIntegral(Context, SourceLoc, Lhs, Rhs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void NondeterministicPointerIterationOrderCheck::check(
TemplateArgs[0].getAsType()->isPointerType();

if (IsAlgoArgPointer) {
SourceRange R = RangeInit->getSourceRange();
const SourceRange R = RangeInit->getSourceRange();
diag(R.getBegin(), "iteration of pointers is nondeterministic") << R;
}
}
Expand All @@ -69,7 +69,7 @@ void NondeterministicPointerIterationOrderCheck::check(
const auto *SortPointers = Result.Nodes.getNodeAs<Stmt>("sortsemantic");

if ((SortPointers) && !(SortPointers->getBeginLoc().isMacroID())) {
SourceRange R = SortPointers->getSourceRange();
const SourceRange R = SortPointers->getSourceRange();
diag(R.getBegin(), "sorting pointers is nondeterministic") << R;
}
}
Expand Down
Loading