forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 6
Dependent base qualifier-id #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ytuo
wants to merge
4
commits into
Bareflank:bsl-tidy
Choose a base branch
from
ytuo:dependent-base
base: bsl-tidy
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
76 changes: 76 additions & 0 deletions
76
clang-tools-extra/clang-tidy/bsl/DependentBaseNameCheck.cpp
This file contains hidden or 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,76 @@ | ||
| //===--- DependentBaseNameCheck.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 "DependentBaseNameCheck.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
|
||
| using namespace clang::ast_matchers; | ||
|
|
||
| namespace clang { | ||
| namespace tidy { | ||
| namespace bsl { | ||
|
|
||
| AST_MATCHER(CXXRecordDecl, hasAnyDependentBases) { | ||
| return Node.hasAnyDependentBases(); | ||
| } | ||
|
|
||
| AST_MATCHER(VarDecl, dependentNameType) { | ||
| return isa<DependentNameType>(Node.getType()); | ||
| } | ||
|
|
||
| void DependentBaseNameCheck::registerMatchers(MatchFinder *Finder) { | ||
| // Finder->addMatcher(cxxThisExpr().bind("this"), this); | ||
| Finder->addMatcher( | ||
| varDecl(unless(anyOf(dependentNameType(), hasType(elaboratedType()))), | ||
| hasAncestor(cxxRecordDecl(hasAnyDependentBases()))) | ||
| .bind("var"), | ||
| this); | ||
|
|
||
| Finder->addMatcher( | ||
| declRefExpr(hasAncestor(cxxRecordDecl(hasAnyDependentBases()))) | ||
| .bind("this"), | ||
| this); | ||
| // memberexpr | ||
| } | ||
|
|
||
| void DependentBaseNameCheck::check(const MatchFinder::MatchResult &Result) { | ||
| const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("var"); | ||
| if (MatchedDecl) { | ||
| // if (isa<ElaboratedType>(MatchedDecl->getType())) { | ||
| diag(MatchedDecl->getLocation(), | ||
| "in a class template with a dependent base, any name that may be " | ||
| "found in that dependent base shall be referred to using a " | ||
| "qualified-id or this->."); | ||
| } | ||
|
|
||
| // getBaseTypeIdentifier | ||
|
|
||
| // ok | ||
| const auto *MatchedExpr = Result.Nodes.getNodeAs<DeclRefExpr>("this"); | ||
| if (MatchedExpr) { | ||
| if (!MatchedExpr->hasQualifier()) { | ||
| diag(MatchedExpr->getLocation(), | ||
| "in a class template with a dependent base, any name that may be " | ||
| "found in that dependent base shall be referred to using a " | ||
| "qualified-id or this->."); | ||
| } | ||
| } | ||
|
|
||
| // const auto *MatchedExpr = Result.Nodes.getNodeAs<CXXThisExpr>("this"); | ||
| // if (MatchedExpr) { | ||
| // diag(MatchedExpr->getLocation(), "this"); | ||
| // if (!MatchedExpr->isImplicit()) { | ||
| // diag(MatchedExpr->getLocation(), "not implicit"); | ||
| // } | ||
| // } | ||
| } | ||
|
|
||
| } // namespace bsl | ||
| } // namespace tidy | ||
| } // namespace clang |
This file contains hidden or 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 @@ | ||
| //===--- DependentBaseNameCheck.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_BSL_DEPENDENTBASENAMECHECK_H | ||
| #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BSL_DEPENDENTBASENAMECHECK_H | ||
|
|
||
| #include "../ClangTidyCheck.h" | ||
|
|
||
| namespace clang { | ||
| namespace tidy { | ||
| namespace bsl { | ||
|
|
||
| /// Checks that names in a class template found in its dependent base are | ||
| /// referred to using a qualified-id or this->. | ||
| /// | ||
| /// For the user-facing documentation see: | ||
| /// http://clang.llvm.org/extra/clang-tidy/checks/bsl-dependent-base-name.html | ||
| class DependentBaseNameCheck : public ClangTidyCheck { | ||
| public: | ||
| DependentBaseNameCheck(StringRef Name, ClangTidyContext *Context) | ||
| : ClangTidyCheck(Name, Context) {} | ||
| void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
| void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
| }; | ||
|
|
||
| } // namespace bsl | ||
| } // namespace tidy | ||
| } // namespace clang | ||
|
|
||
| #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BSL_DEPENDENTBASENAMECHECK_H | ||
This file contains hidden or 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
6 changes: 6 additions & 0 deletions
6
clang-tools-extra/docs/clang-tidy/checks/bsl-dependent-base-name.rst
This file contains hidden or 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,6 @@ | ||
| .. title:: clang-tidy - bsl-dependent-base-name | ||
|
|
||
| bsl-dependent-base-name | ||
| ======================= | ||
|
|
||
| FIXME: Describe what patterns does the check detect and why. Give examples. |
This file contains hidden or 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
80 changes: 80 additions & 0 deletions
80
clang-tools-extra/test/clang-tidy/checkers/bsl-dependent-base-name.cpp
This file contains hidden or 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,80 @@ | ||
| // RUN: %check_clang_tidy %s bsl-dependent-base-name %t | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'f' is insufficiently awesome [bsl-dependent-base-name] | ||
| typedef int32_t TYPE; | ||
| void g ( ); | ||
|
|
||
| template <typename T> | ||
| class B; | ||
|
|
||
| int var; | ||
|
|
||
| template <typename T> | ||
| class A : public B<T> | ||
| { | ||
| void f1 ( ) | ||
| { | ||
| TYPE t = 0; // Non-compliant Example 1 | ||
| g ( ); // Non-compliant Example 2 | ||
| var = 0; // Non-compliant?? use ::var = 0? | ||
|
ytuo marked this conversation as resolved.
|
||
| } | ||
|
|
||
| void f2 ( ) | ||
| { | ||
| ::TYPE t1 = 0; // Compliant - explicit use global TYPE | ||
| ::g ( ); // Compliant - explicit use global func | ||
| typename B<T>::TYPE t2 = 0; // Compliant - explicit use base TYPE | ||
| this->g ( ); // Compliant - explicit use base "g" | ||
| } | ||
| }; | ||
|
|
||
| // All compliant | ||
| template <typename T> | ||
| class B | ||
| { | ||
| public: | ||
| typedef T TYPE; | ||
| void g ( ); | ||
| }; | ||
|
|
||
| template class A<int32_t>; | ||
|
|
||
|
|
||
| class C {}; | ||
|
|
||
| template <typename T> | ||
| class D : public C | ||
| { | ||
| void f1 ( ) | ||
| { | ||
| TYPE t = 0; | ||
| g ( ); | ||
| } | ||
| void f2 ( ) | ||
| { | ||
| ::TYPE t1 = 0; | ||
| ::g ( ); | ||
| typename B<T>::TYPE t2 = 0; | ||
| this->g ( ); | ||
| } | ||
| }; | ||
|
|
||
| class E : C | ||
| { | ||
| void f1 ( ) | ||
| { | ||
| TYPE t = 0; | ||
| g ( ); | ||
| // A a = a(); | ||
| // a->f1(); | ||
| } | ||
| void f2 ( ) | ||
| { | ||
| ::TYPE t1 = 0; | ||
| ::g ( ); | ||
| // typename B<T>::TYPE t2 = 0; // error | ||
| // this->g ( ); // error (???) | ||
| } | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add
isLanguageVersionSupportedoverride