-
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 checks for global
resolved_id
s and fix some assorted…
… clang-tidy errors.
- Loading branch information
Showing
10 changed files
with
170 additions
and
14 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
75 changes: 75 additions & 0 deletions
75
tools/clang-tidy-plugin/StaticResolvedIdConstantsCheck.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,75 @@ | ||
#include "StaticResolvedIdConstantsCheck.h" | ||
#include "Utils.h" | ||
#include <unordered_map> | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::cata | ||
{ | ||
|
||
void StaticResolvedIdConstantsCheck::registerMatchers( MatchFinder *Finder ) | ||
{ | ||
Finder->addMatcher( | ||
varDecl( | ||
hasType( | ||
namedDecl( | ||
anyOf( | ||
hasName( "resolved_id" ), | ||
typedefNameDecl( | ||
hasType( | ||
hasCanonicalType( | ||
hasDeclaration( namedDecl( hasName( "resolved_id" ) ) ) | ||
) | ||
) | ||
) | ||
) | ||
).bind( "typeDecl" ) | ||
) | ||
).bind( "varDecl" ), | ||
this | ||
); | ||
} | ||
|
||
static void CheckConstructor( StaticResolvedIdConstantsCheck &Check, | ||
const MatchFinder::MatchResult &Result ) | ||
{ | ||
const VarDecl *ResolvedIdVarDecl = Result.Nodes.getNodeAs<VarDecl>( "varDecl" ); | ||
const TypeDecl *ResolvedIdTypeDecl = Result.Nodes.getNodeAs<TypeDecl>( "typeDecl" ); | ||
if( !ResolvedIdVarDecl || !ResolvedIdTypeDecl ) { | ||
return; | ||
} | ||
|
||
const VarDecl *PreviousDecl = dyn_cast_or_null<VarDecl>( ResolvedIdVarDecl->getPreviousDecl() ); | ||
|
||
if( PreviousDecl ) { | ||
// Only complain about each variable once | ||
return; | ||
} | ||
|
||
std::string Adjective; | ||
|
||
if( ResolvedIdVarDecl->hasGlobalStorage() ) { | ||
Adjective = "Global"; | ||
} | ||
|
||
if( ResolvedIdVarDecl->isStaticDataMember() || ResolvedIdVarDecl->isStaticLocal() ) { | ||
Adjective = "Static"; | ||
} | ||
|
||
if( Adjective.empty() ) { | ||
return; | ||
} | ||
|
||
Check.diag( | ||
ResolvedIdVarDecl->getBeginLoc(), | ||
"%2 declaration of %0 is dangerous because %1 is a specialization of resolved_id and it " | ||
"will not update automatically when game data changes. Consider switching to a string_id." | ||
) << ResolvedIdVarDecl << ResolvedIdTypeDecl << Adjective; | ||
} | ||
|
||
void StaticResolvedIdConstantsCheck::check( const MatchFinder::MatchResult &Result ) | ||
{ | ||
CheckConstructor( *this, Result ); | ||
} | ||
|
||
} // namespace clang::tidy::cata |
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,33 @@ | ||
#ifndef CATA_TOOLS_CLANG_TIDY_PLUGIN_STATICRESOLVEDIDCONSTANTSCHECK_H | ||
#define CATA_TOOLS_CLANG_TIDY_PLUGIN_STATICRESOLVEDIDCONSTANTSCHECK_H | ||
|
||
#include <clang/ASTMatchers/ASTMatchFinder.h> | ||
#include <llvm/ADT/StringRef.h> | ||
|
||
#include <clang-tidy/ClangTidy.h> | ||
#include <clang-tidy/ClangTidyCheck.h> | ||
|
||
namespace clang | ||
{ | ||
|
||
namespace tidy | ||
{ | ||
class ClangTidyContext; | ||
|
||
namespace cata | ||
{ | ||
|
||
class StaticResolvedIdConstantsCheck : public ClangTidyCheck | ||
{ | ||
public: | ||
StaticResolvedIdConstantsCheck( 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_STATICRESOLVEDIDCONSTANTSCHECK_H |
41 changes: 41 additions & 0 deletions
41
tools/clang-tidy-plugin/test/static-resolved_id-constants.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,41 @@ | ||
// RUN: %check_clang_tidy -allow-stdinc %s cata-static-resolved_id-constants %t -- --load=%cata_plugin -- -isystem %cata_include | ||
|
||
#include "type_id.h" | ||
|
||
extern resolved_furn_id f_hay; | ||
// CHECK-MESSAGES: warning: Global declaration of 'f_hay' is dangerous because 'resolved_furn_id' is a specialization of resolved_id and it will not update automatically when game data changes. Consider switching to a string_id. [cata-static-resolved_id-constants] | ||
|
||
// No warning for second decl os same variable. | ||
resolved_furn_id f_hay; | ||
|
||
static resolved_id<furn_t> f_ball_mach; | ||
// CHECK-MESSAGES: warning: Global declaration of 'f_ball_mach' is dangerous because 'resolved_id<furn_t>' is a specialization of resolved_id and it will not update automatically when game data changes. Consider switching to a string_id. [cata-static-resolved_id-constants] | ||
|
||
namespace foo | ||
{ | ||
|
||
const resolved_furn_id f_dahlia; | ||
// CHECK-MESSAGES: warning: Global declaration of 'f_dahlia' is dangerous because 'resolved_furn_id' is a specialization of resolved_id and it will not update automatically when game data changes. Consider switching to a string_id. [cata-static-resolved_id-constants] | ||
|
||
} // namespace foo | ||
|
||
class A | ||
{ | ||
static resolved_furn_id f_bluebell; | ||
// CHECK-MESSAGES: warning: Static declaration of 'f_bluebell' is dangerous because 'resolved_furn_id' is a specialization of resolved_id and it will not update automatically when game data changes. Consider switching to a string_id. [cata-static-resolved_id-constants] | ||
|
||
// No warning for regular class data members | ||
resolved_furn_id my_furn; | ||
}; | ||
|
||
void f() | ||
{ | ||
static resolved_furn_id f_floor_canvas; | ||
// CHECK-MESSAGES: warning: Static declaration of 'f_floor_canvas' is dangerous because 'resolved_furn_id' is a specialization of resolved_id and it will not update automatically when game data changes. Consider switching to a string_id. [cata-static-resolved_id-constants] | ||
|
||
// No warning for regular local variables | ||
resolved_furn_id f; | ||
|
||
// No warning for other types | ||
static int i; | ||
} |