This repository was archived by the owner on Nov 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Add clang-tidy checker to use free functions for casting #103
Closed
ArtemySkrebkov
wants to merge
3
commits into
intel:npu/release/18.x
from
ArtemySkrebkov:use-free-function-variants
Closed
Changes from 2 commits
Commits
Show all changes
3 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
79 changes: 79 additions & 0 deletions
79
clang-tools-extra/clang-tidy/misc/UseFreeFunctionVariantsCheck.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,79 @@ | ||
| //===--- UseFreeFunctionVariantsCheck.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 "UseFreeFunctionVariantsCheck.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/ASTMatchers/ASTMatchFinder.h" | ||
| #include "clang/ASTMatchers/ASTMatchers.h" | ||
| #include "clang/Basic/SourceManager.h" | ||
| #include "clang/Lex/Lexer.h" | ||
| #include <iostream> | ||
|
|
||
| using namespace clang::ast_matchers; | ||
|
|
||
| namespace clang::tidy::misc { | ||
|
|
||
| void UseFreeFunctionVariantsCheck::registerMatchers(MatchFinder *Finder) { | ||
| Finder->addMatcher(cxxMemberCallExpr(on(expr().bind("base")), | ||
| callee(cxxMethodDecl(hasAnyName("isa", "dyn_cast", "cast", "dyn_cast_or_null"))) | ||
| ).bind("castCall"), | ||
| this); | ||
| } | ||
|
|
||
| // TODO(askrebko): Add checker for types wrapped in pointers and optional | ||
| void UseFreeFunctionVariantsCheck::check( | ||
| const MatchFinder::MatchResult &Result) { | ||
| const auto* CallExpr = Result.Nodes.getNodeAs<CXXMemberCallExpr>("castCall"); | ||
| const auto* BaseExpr = Result.Nodes.getNodeAs<Expr>("base"); | ||
|
|
||
| if (!CallExpr || !BaseExpr) { | ||
| return; | ||
| } | ||
| SourceManager &SM = *Result.SourceManager; | ||
| LangOptions LangOpts = getLangOpts(); | ||
|
|
||
| std::string BaseStr = Lexer::getSourceText( | ||
| CharSourceRange::getTokenRange(BaseExpr->getSourceRange()), SM, LangOpts).str(); | ||
|
|
||
| const auto* MethodDecl = CallExpr->getMethodDecl(); | ||
| if (!MethodDecl->getTemplateSpecializationArgs()) { | ||
| return; | ||
| } | ||
|
|
||
| std::string TemplateArgStr; | ||
| for (const auto& Arg : MethodDecl->getTemplateSpecializationArgs()->asArray()) { | ||
| if (Arg.getKind() == TemplateArgument::ArgKind::Type) { | ||
| PrintingPolicy Policy(LangOpts); | ||
| Policy.adjustForCPlusPlus(); | ||
| if (!TemplateArgStr.empty()) { | ||
| TemplateArgStr += ", "; | ||
| } | ||
| TemplateArgStr += Arg.getAsType().getAsString(Policy); | ||
| } else if (Arg.getKind() == TemplateArgument::ArgKind::Pack) { | ||
| for (const auto& PackArg : Arg.pack_elements()) { | ||
| PrintingPolicy Policy(LangOpts); | ||
| Policy.adjustForCPlusPlus(); | ||
| if (!TemplateArgStr.empty()) { | ||
| TemplateArgStr += ", "; | ||
| } | ||
| TemplateArgStr += PackArg.getAsType().getAsString(Policy); | ||
| } | ||
| } else { | ||
| std::cout << "Unsupported template argument kind: " << Arg.getKind() << std::endl; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| std::string FuncName = MethodDecl->getNameAsString(); | ||
|
|
||
| std::string Replacement = "mlir::" + FuncName + "<" + TemplateArgStr + ">(" + BaseStr + ")"; | ||
| diag(CallExpr->getBeginLoc(), "Replace 'type.isa<T>()' with 'mlir::isa<T>(type)'") | ||
| << FixItHint::CreateReplacement(CallExpr->getSourceRange(), Replacement); | ||
| } | ||
|
|
||
| } // namespace clang::tidy::misc | ||
30 changes: 30 additions & 0 deletions
30
clang-tools-extra/clang-tidy/misc/UseFreeFunctionVariantsCheck.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //===--- UseFreeFunctionVariantsCheck.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_MISC_USEFREEFUNCTIONVARIANTSCHECK_H | ||
| #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEFREEFUNCTIONVARIANTSCHECK_H | ||
|
|
||
| #include "../ClangTidyCheck.h" | ||
|
|
||
| namespace clang::tidy::misc { | ||
|
|
||
| /// FIXME: Write a short description. | ||
| /// | ||
| /// For the user-facing documentation see: | ||
| /// http://clang.llvm.org/extra/clang-tidy/checks/misc/use-free-function-variants.html | ||
| class UseFreeFunctionVariantsCheck : public ClangTidyCheck { | ||
| public: | ||
| UseFreeFunctionVariantsCheck(StringRef Name, ClangTidyContext *Context) | ||
| : ClangTidyCheck(Name, Context) {} | ||
| void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
| void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
| }; | ||
|
|
||
| } // namespace clang::tidy::misc | ||
|
|
||
| #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEFREEFUNCTIONVARIANTSCHECK_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
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/misc/use-free-function-variants.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 - misc-use-free-function-variants | ||
|
|
||
| misc-use-free-function-variants | ||
| =============================== | ||
|
|
||
| FIXME: Describe what patterns does the check detect and why. Give examples. |
14 changes: 14 additions & 0 deletions
14
clang-tools-extra/test/clang-tidy/checkers/misc/use-free-function-variants.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,14 @@ | ||
| // RUN: %check_clang_tidy %s misc-use-free-function-variants %t | ||
|
|
||
| // FIXME: Add something that triggers the check here. | ||
| void f(); | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'f' is insufficiently awesome [misc-use-free-function-variants] | ||
|
|
||
| // FIXME: Verify the applied fix. | ||
| // * Make the CHECK patterns specific enough and try to make verified lines | ||
| // unique to avoid incorrect matches. | ||
| // * Use {{}} for regular expressions. | ||
| // CHECK-FIXES: {{^}}void awesome_f();{{$}} | ||
|
|
||
| // FIXME: Add something that doesn't trigger the check here. | ||
| void awesome_f2(); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.