forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'llvm:main' into llvmgh-101657
- Loading branch information
Showing
318 changed files
with
8,824 additions
and
3,638 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
clang-tools-extra/clang-tidy/bugprone/IncorrectEnableSharedFromThisCheck.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//===--- IncorrectEnableSharedFromThisCheck.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 "IncorrectEnableSharedFromThisCheck.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/AST/DeclCXX.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
void IncorrectEnableSharedFromThisCheck::registerMatchers(MatchFinder *Finder) { | ||
const auto EnableSharedFromThis = | ||
cxxRecordDecl(hasName("enable_shared_from_this"), isInStdNamespace()); | ||
const auto QType = hasCanonicalType(hasDeclaration( | ||
cxxRecordDecl( | ||
anyOf(EnableSharedFromThis.bind("enable_rec"), | ||
cxxRecordDecl(hasAnyBase(cxxBaseSpecifier( | ||
isPublic(), hasType(hasCanonicalType( | ||
hasDeclaration(EnableSharedFromThis)))))))) | ||
.bind("base_rec"))); | ||
Finder->addMatcher( | ||
cxxRecordDecl( | ||
unless(isExpansionInSystemHeader()), | ||
hasDirectBase(cxxBaseSpecifier(unless(isPublic()), hasType(QType)) | ||
.bind("base"))) | ||
.bind("derived"), | ||
this); | ||
} | ||
|
||
void IncorrectEnableSharedFromThisCheck::check( | ||
const MatchFinder::MatchResult &Result) { | ||
const auto *BaseSpec = Result.Nodes.getNodeAs<CXXBaseSpecifier>("base"); | ||
const auto *Base = Result.Nodes.getNodeAs<CXXRecordDecl>("base_rec"); | ||
const auto *Derived = Result.Nodes.getNodeAs<CXXRecordDecl>("derived"); | ||
const bool IsEnableSharedFromThisDirectBase = | ||
Result.Nodes.getNodeAs<CXXRecordDecl>("enable_rec") == Base; | ||
const bool HasWrittenAccessSpecifier = | ||
BaseSpec->getAccessSpecifierAsWritten() != AS_none; | ||
const auto ReplacementRange = CharSourceRange( | ||
SourceRange(BaseSpec->getBeginLoc()), HasWrittenAccessSpecifier); | ||
const llvm::StringRef Replacement = | ||
HasWrittenAccessSpecifier ? "public" : "public "; | ||
const FixItHint Hint = | ||
IsEnableSharedFromThisDirectBase | ||
? FixItHint::CreateReplacement(ReplacementRange, Replacement) | ||
: FixItHint(); | ||
diag(Derived->getLocation(), | ||
"%2 is not publicly inheriting from " | ||
"%select{%1 which inherits from |}0'std::enable_shared_" | ||
"from_this', " | ||
"which will cause unintended behaviour " | ||
"when using 'shared_from_this'; make the inheritance " | ||
"public", | ||
DiagnosticIDs::Warning) | ||
<< IsEnableSharedFromThisDirectBase << Base << Derived << Hint; | ||
} | ||
|
||
} // namespace clang::tidy::bugprone |
35 changes: 35 additions & 0 deletions
35
clang-tools-extra/clang-tidy/bugprone/IncorrectEnableSharedFromThisCheck.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//===--- IncorrectEnableSharedFromThisCheck.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_INCORRECTENABLESHAREDFROMTHISCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INCORRECTENABLESHAREDFROMTHISCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
/// Detect classes or structs that do not publicly inherit from | ||
/// ``std::enable_shared_from_this``, because unintended behavior will | ||
/// otherwise occur when calling ``shared_from_this``. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/incorrect-enable-shared-from-this.html | ||
class IncorrectEnableSharedFromThisCheck : public ClangTidyCheck { | ||
public: | ||
IncorrectEnableSharedFromThisCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus11; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::bugprone | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INCORRECTENABLESHAREDFROMTHISCHECK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.