-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 test filenames
Enforce the rule that test files should be named *_test.cpp.
- Loading branch information
Showing
5 changed files
with
91 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,56 @@ | ||
#include "TestFilenameCheck.h" | ||
|
||
#include <clang/Lex/PPCallbacks.h> | ||
#include <clang/Lex/Preprocessor.h> | ||
#include <clang/Lex/Token.h> | ||
#include <string> | ||
|
||
#include "clang/Frontend/CompilerInstance.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang | ||
{ | ||
namespace tidy | ||
{ | ||
namespace cata | ||
{ | ||
|
||
class TestFilenameCallbacks : public PPCallbacks | ||
{ | ||
public: | ||
TestFilenameCallbacks( TestFilenameCheck *Check, CompilerInstance *Compiler ) : | ||
Check( Check ), Compiler( Compiler ) {} | ||
|
||
void MacroExpands( const Token &MacroNameTok, | ||
const MacroDefinition &, | ||
SourceRange Range, | ||
const MacroArgs * ) override { | ||
StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName(); | ||
|
||
if( MacroName == "TEST_CASE" ) { | ||
SourceManager &SM = Compiler->getSourceManager(); | ||
StringRef Filename = SM.getBufferName( Range.getBegin() ); | ||
bool IsTestFilename = Filename.endswith( "_test.cpp" ); | ||
|
||
if( !IsTestFilename ) { | ||
Check->diag( Range.getBegin(), | ||
"Files containing a test definition should have a filename " | ||
"ending in '_test.cpp'." ); | ||
} | ||
} | ||
} | ||
private: | ||
TestFilenameCheck *Check; | ||
CompilerInstance *Compiler; | ||
}; | ||
|
||
void TestFilenameCheck::registerPPCallbacks( CompilerInstance &Compiler ) | ||
{ | ||
Compiler.getPreprocessor().addPPCallbacks( | ||
llvm::make_unique<TestFilenameCallbacks>( this, &Compiler ) ); | ||
} | ||
|
||
} // namespace cata | ||
} // namespace tidy | ||
} // namespace clang |
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,26 @@ | ||
#ifndef CATA_TOOLS_CLANG_TIDY_TESTFILENAMECHECK_H | ||
#define CATA_TOOLS_CLANG_TIDY_TESTFILENAMECHECK_H | ||
|
||
#include "ClangTidy.h" | ||
|
||
namespace clang | ||
{ | ||
namespace tidy | ||
{ | ||
namespace cata | ||
{ | ||
|
||
class TestFilenameCheck : public ClangTidyCheck | ||
{ | ||
public: | ||
TestFilenameCheck( StringRef Name, ClangTidyContext *Context ) | ||
: ClangTidyCheck( Name, Context ) {} | ||
|
||
void registerPPCallbacks( CompilerInstance &Compiler ) override; | ||
}; | ||
|
||
} // namespace cata | ||
} // namespace tidy | ||
} // namespace clang | ||
|
||
#endif // CATA_TOOLS_CLANG_TIDY_TESTFILENAMECHECK_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,6 @@ | ||
// RUN: %check_clang_tidy %s cata-test-filename %t -- -plugins=%cata_plugin -- -isystem %cata_include | ||
|
||
#define TEST_CASE(name) | ||
|
||
TEST_CASE( "test_name" ) | ||
// CHECK-MESSAGES: warning: Files containing a test definition should have a filename ending in '_test.cpp'. [cata-test-filename] |