Skip to content

Commit

Permalink
Manually merged master:b54aa053d3a into amd-gfx:e9c2e262572
Browse files Browse the repository at this point in the history
Local branch amd-gfx e9c2e26 Manually merged master:25afe91fd1b into amd-gfx:66dfcd6fd7a
Remote branch master b54aa05 Update clang-interpreter example to incorporate changes in ce2207a.

Change-Id: I52871585037084c85d9c5e625014fac74f0d18ac
  • Loading branch information
perlfu committed Jan 22, 2020
2 parents e9c2e26 + b54aa05 commit 7ebe085
Show file tree
Hide file tree
Showing 1,471 changed files with 62,546 additions and 20,572 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ void NoInternalDependenciesCheck::check(const MatchFinder::MatchResult &Result)
const auto *InternalDependency =
Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("InternalDep");

diag(InternalDependency->getBeginLoc(),
SourceLocation LocAtFault =
Result.SourceManager->getSpellingLoc(InternalDependency->getBeginLoc());

if (!LocAtFault.isValid())
return;

diag(LocAtFault,
"do not reference any 'internal' namespaces; those implementation "
"details are reserved to Abseil");
}
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ namespace bugprone {

void BranchCloneCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
ifStmt(stmt().bind("if"),
ifStmt(unless(allOf(isConstexpr(), isInTemplateInstantiation())),
stmt().bind("if"),
hasParent(stmt(unless(ifStmt(hasElse(equalsBoundNode("if")))))),
hasElse(stmt().bind("else"))),
this);
Expand Down
6 changes: 6 additions & 0 deletions clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
#include "MacroParenthesesCheck.h"
#include "MacroRepeatedSideEffectsCheck.h"
#include "MisplacedOperatorInStrlenInAllocCheck.h"
#include "MisplacedPointerArithmeticInAllocCheck.h"
#include "MisplacedWideningCastCheck.h"
#include "MoveForwardingReferenceCheck.h"
#include "MultipleStatementMacroCheck.h"
#include "NotNullTerminatedResultCheck.h"
#include "ParentVirtualCallCheck.h"
#include "PosixReturnCheck.h"
#include "ReservedIdentifierCheck.h"
#include "SignedCharMisuseCheck.h"
#include "SizeofContainerCheck.h"
#include "SizeofExpressionCheck.h"
Expand Down Expand Up @@ -106,6 +108,8 @@ class BugproneModule : public ClangTidyModule {
"bugprone-macro-repeated-side-effects");
CheckFactories.registerCheck<MisplacedOperatorInStrlenInAllocCheck>(
"bugprone-misplaced-operator-in-strlen-in-alloc");
CheckFactories.registerCheck<MisplacedPointerArithmeticInAllocCheck>(
"bugprone-misplaced-pointer-arithmetic-in-alloc");
CheckFactories.registerCheck<MisplacedWideningCastCheck>(
"bugprone-misplaced-widening-cast");
CheckFactories.registerCheck<MoveForwardingReferenceCheck>(
Expand All @@ -120,6 +124,8 @@ class BugproneModule : public ClangTidyModule {
"bugprone-parent-virtual-call");
CheckFactories.registerCheck<PosixReturnCheck>(
"bugprone-posix-return");
CheckFactories.registerCheck<ReservedIdentifierCheck>(
"bugprone-reserved-identifier");
CheckFactories.registerCheck<SignedCharMisuseCheck>(
"bugprone-signed-char-misuse");
CheckFactories.registerCheck<SizeofContainerCheck>(
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ add_clang_library(clangTidyBugproneModule
MacroParenthesesCheck.cpp
MacroRepeatedSideEffectsCheck.cpp
MisplacedOperatorInStrlenInAllocCheck.cpp
MisplacedPointerArithmeticInAllocCheck.cpp
MisplacedWideningCastCheck.cpp
MoveForwardingReferenceCheck.cpp
MultipleStatementMacroCheck.cpp
NotNullTerminatedResultCheck.cpp
ParentVirtualCallCheck.cpp
PosixReturnCheck.cpp
ReservedIdentifierCheck.cpp
SignedCharMisuseCheck.cpp
SizeofContainerCheck.cpp
SizeofExpressionCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//===--- MisplacedPointerArithmeticInAllocCheck.cpp - clang-tidy-----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "MisplacedPointerArithmeticInAllocCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace bugprone {

void MisplacedPointerArithmeticInAllocCheck::registerMatchers(
MatchFinder *Finder) {
const auto AllocFunc = functionDecl(
anyOf(hasName("::malloc"), hasName("std::malloc"), hasName("::alloca"),
hasName("::calloc"), hasName("std::calloc"), hasName("::realloc"),
hasName("std::realloc")));

const auto AllocFuncPtr =
varDecl(hasType(isConstQualified()),
hasInitializer(ignoringParenImpCasts(
declRefExpr(hasDeclaration(AllocFunc)))));

const auto AdditiveOperator =
binaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("-")));

const auto IntExpr = expr(hasType(isInteger()));

const auto AllocCall = callExpr(callee(decl(anyOf(AllocFunc, AllocFuncPtr))));

Finder->addMatcher(
binaryOperator(
AdditiveOperator,
hasLHS(anyOf(AllocCall, castExpr(hasSourceExpression(AllocCall)))),
hasRHS(IntExpr))
.bind("PtrArith"),
this);

const auto New = cxxNewExpr(unless(isArray()));

Finder->addMatcher(binaryOperator(AdditiveOperator,
hasLHS(anyOf(New, castExpr(New))),
hasRHS(IntExpr))
.bind("PtrArith"),
this);

const auto ArrayNew = cxxNewExpr(isArray());

Finder->addMatcher(binaryOperator(AdditiveOperator,
hasLHS(anyOf(ArrayNew, castExpr(ArrayNew))),
hasRHS(IntExpr))
.bind("PtrArith"),
this);
}

void MisplacedPointerArithmeticInAllocCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *PtrArith = Result.Nodes.getNodeAs<BinaryOperator>("PtrArith");
const Expr *AllocExpr = PtrArith->getLHS()->IgnoreParenCasts();
std::string CallName;

if (const auto *Call = dyn_cast<CallExpr>(AllocExpr)) {
const NamedDecl *Func = Call->getDirectCallee();
if (!Func) {
Func = cast<NamedDecl>(Call->getCalleeDecl());
}
CallName = Func->getName().str();
} else {
const auto *New = cast<CXXNewExpr>(AllocExpr);
if (New->isArray()) {
CallName = "operator new[]";
} else {
const auto *CtrE = New->getConstructExpr();
if (!CtrE->getArg(CtrE->getNumArgs() - 1)
->getType()
->isIntegralOrEnumerationType())
return;
CallName = "operator new";
}
}

const SourceRange OldRParen = SourceRange(PtrArith->getLHS()->getEndLoc());
const StringRef RParen =
Lexer::getSourceText(CharSourceRange::getTokenRange(OldRParen),
*Result.SourceManager, getLangOpts());
const SourceLocation NewRParen = Lexer::getLocForEndOfToken(
PtrArith->getEndLoc(), 0, *Result.SourceManager, getLangOpts());

diag(PtrArith->getBeginLoc(),
"arithmetic operation is applied to the result of %0() instead of its "
"size-like argument")
<< CallName << FixItHint::CreateRemoval(OldRParen)
<< FixItHint::CreateInsertion(NewRParen, RParen);
}

} // namespace bugprone
} // namespace tidy
} // namespace clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===--- MisplacedPointerArithmeticInAllocCheck.h - clang-tidy---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MISPLACED_OPERATOR_IN_ALLOC_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MISPLACED_OPERATOR_IN_ALLOC_H

#include "../ClangTidyCheck.h"

namespace clang {
namespace tidy {
namespace bugprone {

/// Finds cases where an integer is added to or subracted from the result of a
/// memory allocation function instead of its argument.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-misplaced-operator-in-alloc.html
class MisplacedPointerArithmeticInAllocCheck : public ClangTidyCheck {
public:
MisplacedPointerArithmeticInAllocCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};

} // namespace bugprone
} // namespace tidy
} // namespace clang

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MISPLACED_OPERATOR_IN_ALLOC_H
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee,

if (CallRange.isValid()) {
const std::string TypeName =
TypeParmDecl->getIdentifier()
(TypeParmDecl->getIdentifier() && !TypeParmDecl->isImplicit())
? TypeParmDecl->getName().str()
: (llvm::Twine("decltype(") + ParmVar->getName() + ")").str();

Expand Down
Loading

0 comments on commit 7ebe085

Please sign in to comment.