Skip to content

Commit d43ee69

Browse files
authored
Merge pull request #815 for LLVM and SPIRV-translator pulldown
2 parents c5e6723 + 950bd4c commit d43ee69

File tree

2,222 files changed

+86384
-32444
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,222 files changed

+86384
-32444
lines changed

clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,24 @@ namespace clang {
1717
namespace tidy {
1818
namespace hicpp {
1919

20+
SignedBitwiseCheck::SignedBitwiseCheck(StringRef Name,
21+
ClangTidyContext *Context)
22+
: ClangTidyCheck(Name, Context),
23+
IgnorePositiveIntegerLiterals(
24+
Options.get("IgnorePositiveIntegerLiterals", false)) {}
25+
26+
void SignedBitwiseCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
27+
Options.store(Opts, "IgnorePositiveIntegerLiterals",
28+
IgnorePositiveIntegerLiterals);
29+
}
30+
2031
void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
2132
const auto SignedIntegerOperand =
22-
expr(ignoringImpCasts(hasType(isSignedInteger()))).bind("signed-operand");
33+
(IgnorePositiveIntegerLiterals
34+
? expr(ignoringImpCasts(hasType(isSignedInteger())),
35+
unless(integerLiteral()))
36+
: expr(ignoringImpCasts(hasType(isSignedInteger()))))
37+
.bind("signed-operand");
2338

2439
// The standard [bitmask.types] allows some integral types to be implemented
2540
// as signed types. Exclude these types from diagnosing for bitwise or(|) and

clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ namespace hicpp {
2222
/// http://clang.llvm.org/extra/clang-tidy/checks/hicpp-signed-bitwise.html
2323
class SignedBitwiseCheck : public ClangTidyCheck {
2424
public:
25-
SignedBitwiseCheck(StringRef Name, ClangTidyContext *Context)
26-
: ClangTidyCheck(Name, Context) {}
25+
SignedBitwiseCheck(StringRef Name, ClangTidyContext *Context);
2726
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2827
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
28+
void storeOptions(ClangTidyOptions::OptionMap &Options) override;
29+
30+
private:
31+
bool IgnorePositiveIntegerLiterals;
2932
};
3033

3134
} // namespace hicpp

clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
131131
case Stmt::BinaryOperatorClass:
132132
return cast<BinaryOperator>(Left)->getOpcode() ==
133133
cast<BinaryOperator>(Right)->getOpcode();
134+
case Stmt::UnaryExprOrTypeTraitExprClass:
135+
const auto *LeftUnaryExpr =
136+
cast<UnaryExprOrTypeTraitExpr>(Left);
137+
const auto *RightUnaryExpr =
138+
cast<UnaryExprOrTypeTraitExpr>(Right);
139+
if (LeftUnaryExpr->isArgumentType() && RightUnaryExpr->isArgumentType())
140+
return LeftUnaryExpr->getArgumentType() ==
141+
RightUnaryExpr->getArgumentType();
142+
else if (!LeftUnaryExpr->isArgumentType() &&
143+
!RightUnaryExpr->isArgumentType())
144+
return areEquivalentExpr(LeftUnaryExpr->getArgumentExpr(),
145+
RightUnaryExpr->getArgumentExpr());
146+
147+
return false;
134148
}
135149
}
136150

@@ -604,23 +618,62 @@ static bool retrieveConstExprFromBothSides(const BinaryOperator *&BinOp,
604618
return true;
605619
}
606620

621+
static bool isSameRawIdentifierToken(const Token &T1, const Token &T2,
622+
const SourceManager &SM) {
623+
if (T1.getKind() != T2.getKind())
624+
return false;
625+
if (T1.isNot(tok::raw_identifier))
626+
return true;
627+
if (T1.getLength() != T2.getLength())
628+
return false;
629+
return StringRef(SM.getCharacterData(T1.getLocation()), T1.getLength()) ==
630+
StringRef(SM.getCharacterData(T2.getLocation()), T2.getLength());
631+
}
632+
633+
bool isTokAtEndOfExpr(SourceRange ExprSR, Token T, const SourceManager &SM) {
634+
return SM.getExpansionLoc(ExprSR.getEnd()) == T.getLocation();
635+
}
636+
637+
/// Returns true if both LhsEpxr and RhsExpr are
638+
/// macro expressions and they are expanded
639+
/// from different macros.
607640
static bool areExprsFromDifferentMacros(const Expr *LhsExpr,
608641
const Expr *RhsExpr,
609642
const ASTContext *AstCtx) {
610643
if (!LhsExpr || !RhsExpr)
611644
return false;
612-
613-
SourceLocation LhsLoc = LhsExpr->getExprLoc();
614-
SourceLocation RhsLoc = RhsExpr->getExprLoc();
615-
616-
if (!LhsLoc.isMacroID() || !RhsLoc.isMacroID())
645+
SourceRange Lsr = LhsExpr->getSourceRange();
646+
SourceRange Rsr = RhsExpr->getSourceRange();
647+
if (!Lsr.getBegin().isMacroID() || !Rsr.getBegin().isMacroID())
617648
return false;
618649

619650
const SourceManager &SM = AstCtx->getSourceManager();
620651
const LangOptions &LO = AstCtx->getLangOpts();
621652

622-
return !(Lexer::getImmediateMacroName(LhsLoc, SM, LO) ==
623-
Lexer::getImmediateMacroName(RhsLoc, SM, LO));
653+
std::pair<FileID, unsigned> LsrLocInfo =
654+
SM.getDecomposedLoc(SM.getExpansionLoc(Lsr.getBegin()));
655+
std::pair<FileID, unsigned> RsrLocInfo =
656+
SM.getDecomposedLoc(SM.getExpansionLoc(Rsr.getBegin()));
657+
const llvm::MemoryBuffer *MB = SM.getBuffer(LsrLocInfo.first);
658+
659+
const char *LTokenPos = MB->getBufferStart() + LsrLocInfo.second;
660+
const char *RTokenPos = MB->getBufferStart() + RsrLocInfo.second;
661+
Lexer LRawLex(SM.getLocForStartOfFile(LsrLocInfo.first), LO,
662+
MB->getBufferStart(), LTokenPos, MB->getBufferEnd());
663+
Lexer RRawLex(SM.getLocForStartOfFile(RsrLocInfo.first), LO,
664+
MB->getBufferStart(), RTokenPos, MB->getBufferEnd());
665+
666+
Token LTok, RTok;
667+
do { // Compare the expressions token-by-token.
668+
LRawLex.LexFromRawLexer(LTok);
669+
RRawLex.LexFromRawLexer(RTok);
670+
} while (!LTok.is(tok::eof) && !RTok.is(tok::eof) &&
671+
isSameRawIdentifierToken(LTok, RTok, SM) &&
672+
!isTokAtEndOfExpr(Lsr, LTok, SM) &&
673+
!isTokAtEndOfExpr(Rsr, RTok, SM));
674+
return (!isTokAtEndOfExpr(Lsr, LTok, SM) ||
675+
!isTokAtEndOfExpr(Rsr, RTok, SM)) ||
676+
!isSameRawIdentifierToken(LTok, RTok, SM);
624677
}
625678

626679
static bool areExprsMacroAndNonMacro(const Expr *&LhsExpr,

clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ void UseNodiscardCheck::registerMatchers(MatchFinder *Finder) {
100100
cxxMethodDecl(
101101
allOf(isConst(), isDefinitionOrInline(),
102102
unless(anyOf(
103-
returns(voidType()), isNoReturn(), isOverloadedOperator(),
103+
returns(voidType()),
104+
returns(hasDeclaration(decl(hasAttr(clang::attr::WarnUnusedResult)))),
105+
isNoReturn(), isOverloadedOperator(),
104106
isVariadic(), hasTemplateReturnType(),
105107
hasClassMutableFields(), isConversionOperator(),
106108
hasAttr(clang::attr::WarnUnusedResult),

clang-tools-extra/clang-tidy/performance/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_clang_library(clangTidyPerformanceModule
1111
MoveConstructorInitCheck.cpp
1212
NoexceptMoveConstructorCheck.cpp
1313
PerformanceTidyModule.cpp
14+
TriviallyDestructibleCheck.cpp
1415
TypePromotionInMathFnCheck.cpp
1516
UnnecessaryCopyInitialization.cpp
1617
UnnecessaryValueParamCheck.cpp

clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "MoveConstArgCheck.h"
1919
#include "MoveConstructorInitCheck.h"
2020
#include "NoexceptMoveConstructorCheck.h"
21+
#include "TriviallyDestructibleCheck.h"
2122
#include "TypePromotionInMathFnCheck.h"
2223
#include "UnnecessaryCopyInitialization.h"
2324
#include "UnnecessaryValueParamCheck.h"
@@ -47,6 +48,8 @@ class PerformanceModule : public ClangTidyModule {
4748
"performance-move-constructor-init");
4849
CheckFactories.registerCheck<NoexceptMoveConstructorCheck>(
4950
"performance-noexcept-move-constructor");
51+
CheckFactories.registerCheck<TriviallyDestructibleCheck>(
52+
"performance-trivially-destructible");
5053
CheckFactories.registerCheck<TypePromotionInMathFnCheck>(
5154
"performance-type-promotion-in-math-fn");
5255
CheckFactories.registerCheck<UnnecessaryCopyInitialization>(
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===--- TriviallyDestructibleCheck.cpp - clang-tidy ----------------------===//
2+
//
3+
// 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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "TriviallyDestructibleCheck.h"
10+
#include "../utils/LexerUtils.h"
11+
#include "../utils/Matchers.h"
12+
#include "clang/AST/ASTContext.h"
13+
#include "clang/ASTMatchers/ASTMatchFinder.h"
14+
15+
using namespace clang::ast_matchers;
16+
using namespace clang::ast_matchers::internal;
17+
using namespace clang::tidy::matchers;
18+
19+
namespace clang {
20+
namespace tidy {
21+
namespace performance {
22+
23+
namespace {
24+
25+
AST_MATCHER(Decl, isFirstDecl) { return Node.isFirstDecl(); }
26+
27+
AST_MATCHER_P(CXXRecordDecl, hasBase, Matcher<QualType>, InnerMatcher) {
28+
for (const CXXBaseSpecifier &BaseSpec : Node.bases()) {
29+
QualType BaseType = BaseSpec.getType();
30+
if (InnerMatcher.matches(BaseType, Finder, Builder))
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
} // namespace
37+
38+
void TriviallyDestructibleCheck::registerMatchers(MatchFinder *Finder) {
39+
if (!getLangOpts().CPlusPlus11)
40+
return;
41+
42+
Finder->addMatcher(
43+
cxxDestructorDecl(
44+
isDefaulted(),
45+
unless(anyOf(isFirstDecl(), isVirtual(),
46+
ofClass(cxxRecordDecl(
47+
anyOf(hasBase(unless(isTriviallyDestructible())),
48+
has(fieldDecl(unless(
49+
hasType(isTriviallyDestructible()))))))))))
50+
.bind("decl"),
51+
this);
52+
}
53+
54+
void TriviallyDestructibleCheck::check(const MatchFinder::MatchResult &Result) {
55+
const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXDestructorDecl>("decl");
56+
57+
// Get locations of both first and out-of-line declarations.
58+
SourceManager &SM = *Result.SourceManager;
59+
const auto *FirstDecl = cast<CXXMethodDecl>(MatchedDecl->getFirstDecl());
60+
const SourceLocation FirstDeclEnd = utils::lexer::findNextTerminator(
61+
FirstDecl->getEndLoc(), SM, getLangOpts());
62+
const CharSourceRange SecondDeclRange = CharSourceRange::getTokenRange(
63+
MatchedDecl->getBeginLoc(),
64+
utils::lexer::findNextTerminator(MatchedDecl->getEndLoc(), SM,
65+
getLangOpts()));
66+
if (FirstDeclEnd.isInvalid() || SecondDeclRange.isInvalid())
67+
return;
68+
69+
// Report diagnostic.
70+
diag(FirstDecl->getLocation(),
71+
"class %0 can be made trivially destructible by defaulting the "
72+
"destructor on its first declaration")
73+
<< FirstDecl->getParent()
74+
<< FixItHint::CreateInsertion(FirstDeclEnd, " = default")
75+
<< FixItHint::CreateRemoval(SecondDeclRange);
76+
diag(MatchedDecl->getLocation(), "destructor definition is here",
77+
DiagnosticIDs::Note);
78+
}
79+
80+
} // namespace performance
81+
} // namespace tidy
82+
} // namespace clang
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- TriviallyDestructibleCheck.h - clang-tidy --------------*- C++ -*-===//
2+
//
3+
// 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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_TRIVIALLYDESTRUCTIBLECHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_TRIVIALLYDESTRUCTIBLECHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang {
15+
namespace tidy {
16+
namespace performance {
17+
18+
/// A check that finds classes that would be trivial if not for the defaulted
19+
/// destructors declared out-of-line:
20+
/// struct A: TrivialClass {
21+
/// ~A();
22+
/// TrivialClass trivial_fields;
23+
/// };
24+
/// A::~A() = default;
25+
///
26+
/// For the user-facing documentation see:
27+
/// http://clang.llvm.org/extra/clang-tidy/checks/performance-trivially-destructible.html
28+
class TriviallyDestructibleCheck : public ClangTidyCheck {
29+
public:
30+
TriviallyDestructibleCheck(StringRef Name, ClangTidyContext *Context)
31+
: ClangTidyCheck(Name, Context) {}
32+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
33+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
34+
};
35+
36+
} // namespace performance
37+
} // namespace tidy
38+
} // namespace clang
39+
40+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_TRIVIALLYDESTRUCTIBLECHECK_H

clang-tools-extra/clang-tidy/readability/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ add_clang_library(clangTidyReadabilityModule
1515
InconsistentDeclarationParameterNameCheck.cpp
1616
IsolateDeclarationCheck.cpp
1717
MagicNumbersCheck.cpp
18+
MakeMemberFunctionConstCheck.cpp
1819
MisleadingIndentationCheck.cpp
1920
MisplacedArrayIndexCheck.cpp
2021
NamedParameterCheck.cpp
2122
NamespaceCommentCheck.cpp
2223
NonConstParameterCheck.cpp
2324
ReadabilityTidyModule.cpp
25+
RedundantAccessSpecifiersCheck.cpp
2426
RedundantControlFlowCheck.cpp
2527
RedundantDeclarationCheck.cpp
2628
RedundantFunctionPtrDereferenceCheck.cpp

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,11 @@ static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
691691
if (!Failure.RawUsageLocs.insert(FixLocation.getRawEncoding()).second)
692692
return;
693693

694-
if (!Failure.ShouldFix)
694+
if (!Failure.ShouldFix())
695695
return;
696696

697-
Failure.ShouldFix = utils::rangeCanBeFixed(Range, SourceMgr);
697+
if (!utils::rangeCanBeFixed(Range, SourceMgr))
698+
Failure.FixStatus = IdentifierNamingCheck::ShouldFixStatus::InsideMacro;
698699
}
699700

700701
/// Convenience method when the usage to be added is a NamedDecl
@@ -873,6 +874,16 @@ void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) {
873874
DeclarationNameInfo(Decl->getDeclName(), Decl->getLocation())
874875
.getSourceRange();
875876

877+
const IdentifierTable &Idents = Decl->getASTContext().Idents;
878+
auto CheckNewIdentifier = Idents.find(Fixup);
879+
if (CheckNewIdentifier != Idents.end()) {
880+
const IdentifierInfo *Ident = CheckNewIdentifier->second;
881+
if (Ident->isKeyword(getLangOpts()))
882+
Failure.FixStatus = ShouldFixStatus::ConflictsWithKeyword;
883+
else if (Ident->hasMacroDefinition())
884+
Failure.FixStatus = ShouldFixStatus::ConflictsWithMacroDefinition;
885+
}
886+
876887
Failure.Fixup = std::move(Fixup);
877888
Failure.KindName = std::move(KindName);
878889
addUsage(NamingCheckFailures, Decl, Range);
@@ -935,24 +946,35 @@ void IdentifierNamingCheck::onEndOfTranslationUnit() {
935946
if (Failure.KindName.empty())
936947
continue;
937948

938-
if (Failure.ShouldFix) {
939-
auto Diag = diag(Decl.first, "invalid case style for %0 '%1'")
940-
<< Failure.KindName << Decl.second;
941-
942-
for (const auto &Loc : Failure.RawUsageLocs) {
943-
// We assume that the identifier name is made of one token only. This is
944-
// always the case as we ignore usages in macros that could build
945-
// identifier names by combining multiple tokens.
946-
//
947-
// For destructors, we alread take care of it by remembering the
948-
// location of the start of the identifier and not the start of the
949-
// tilde.
950-
//
951-
// Other multi-token identifiers, such as operators are not checked at
952-
// all.
953-
Diag << FixItHint::CreateReplacement(
954-
SourceRange(SourceLocation::getFromRawEncoding(Loc)),
955-
Failure.Fixup);
949+
if (Failure.ShouldNotify()) {
950+
auto Diag =
951+
diag(Decl.first,
952+
"invalid case style for %0 '%1'%select{|" // Case 0 is empty on
953+
// purpose, because we
954+
// intent to provide a
955+
// fix
956+
"; cannot be fixed because '%3' would conflict with a keyword|"
957+
"; cannot be fixed because '%3' would conflict with a macro "
958+
"definition}2")
959+
<< Failure.KindName << Decl.second
960+
<< static_cast<int>(Failure.FixStatus) << Failure.Fixup;
961+
962+
if (Failure.ShouldFix()) {
963+
for (const auto &Loc : Failure.RawUsageLocs) {
964+
// We assume that the identifier name is made of one token only. This
965+
// is always the case as we ignore usages in macros that could build
966+
// identifier names by combining multiple tokens.
967+
//
968+
// For destructors, we already take care of it by remembering the
969+
// location of the start of the identifier and not the start of the
970+
// tilde.
971+
//
972+
// Other multi-token identifiers, such as operators are not checked at
973+
// all.
974+
Diag << FixItHint::CreateReplacement(
975+
SourceRange(SourceLocation::getFromRawEncoding(Loc)),
976+
Failure.Fixup);
977+
}
956978
}
957979
}
958980
}

0 commit comments

Comments
 (0)