-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clang-tidy check for overflow in constructing energy quantity fro…
…m int
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "UnitOverflowCheck.h" | ||
#include "Utils.h" | ||
|
||
#include <clang/ASTMatchers/ASTMatchers.h> | ||
#include <clang/Basic/Diagnostic.h> | ||
|
||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::cata | ||
{ | ||
|
||
static std::vector<std::pair<std::string_view, std::string_view>> unitFunctions { | ||
{"energy", "from_joule"}, | ||
{"energy", "from_kilojoule"}, | ||
}; | ||
|
||
static std::string_view unitOfFunction( const std::string_view queryFunctionName ) | ||
{ | ||
for( const auto &[unit, functionName] : unitFunctions ) { | ||
if( queryFunctionName == functionName ) { | ||
return unit; | ||
} | ||
} | ||
return "?"; | ||
} | ||
|
||
void UnitOverflowCheck::registerMatchers( ast_matchers::MatchFinder *Finder ) | ||
{ | ||
for( const auto &[unit, functionName] : unitFunctions ) { | ||
Finder->addMatcher( | ||
callExpr( callee( functionDecl( hasName( functionName ) ).bind( "func" ) ), hasArgument( 0, | ||
expr( hasType( | ||
isInteger() ) ).bind( | ||
"arg" ) ) ), | ||
this | ||
); | ||
} | ||
} | ||
|
||
void UnitOverflowCheck::check( const ast_matchers::MatchFinder::MatchResult &Result ) | ||
{ | ||
const FunctionDecl *func = Result.Nodes.getNodeAs<FunctionDecl>( "func" ); | ||
const Expr *arg = Result.Nodes.getNodeAs<Expr>( "arg" ); | ||
if( !func || !arg ) { | ||
return; | ||
} | ||
const QualType type = arg->getType(); | ||
const std::uint64_t width = Result.Context->getTypeInfo( type ).Width; | ||
if( width >= 64 ) { | ||
return; | ||
} | ||
const SourceManager &sourceManager = *Result.SourceManager; | ||
if( sourceManager.getFilename( arg->getBeginLoc() ).ends_with( "src/units.h" ) ) { | ||
return; | ||
} | ||
diag( arg->getBeginLoc(), | ||
"constructing a %0 quantity from '%1' potentially overflows inside '%2' in multiplying with conversion factor" ) | ||
<< unitOfFunction( func->getNameAsString() ) << type.getAsString() << func->getName() | ||
<< FixItHint::CreateReplacement( arg->getSourceRange(), | ||
( Twine( "static_cast<std::int64_t>( " ) + getText( Result, arg ) + " )" ).str() ); | ||
} | ||
|
||
} | ||
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,30 @@ | ||
#ifndef CATA_TOOLS_CLANG_TIDY_PLUGIN_UNITOVERFLOWCHECK_H | ||
#define CATA_TOOLS_CLANG_TIDY_PLUGIN_UNITOVERFLOWCHECK_H | ||
|
||
#include <clang-tidy/ClangTidy.h> | ||
#include <clang-tidy/ClangTidyCheck.h> | ||
#include <clang/ASTMatchers/ASTMatchFinder.h> | ||
|
||
namespace clang | ||
{ | ||
|
||
namespace tidy | ||
{ | ||
class ClangTidyContext; | ||
|
||
namespace cata | ||
{ | ||
|
||
class UnitOverflowCheck : public ClangTidyCheck | ||
{ | ||
public: | ||
UnitOverflowCheck( StringRef Name, ClangTidyContext *Context ) : ClangTidyCheck( Name, Context ) {} | ||
void registerMatchers( ast_matchers::MatchFinder *Finder ) override; | ||
void check( const ast_matchers::MatchFinder::MatchResult &Result ) override; | ||
}; | ||
|
||
} // namespace cata | ||
} //namespace tidy | ||
} // namespace clang | ||
|
||
#endif // CATA_TOOLS_CLANG_TIDY_PLUGIN_UNITOVERFLOWCHECK_H |