Skip to content

Commit

Permalink
Add clang-tidy check for test filenames
Browse files Browse the repository at this point in the history
Enforce the rule that test files should be named *_test.cpp.
  • Loading branch information
jbytheway committed Jan 11, 2020
1 parent ba319e7 commit e312278
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/clang-tidy-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_library(
PointInitializationCheck.cpp
SimplifyPointConstructorsCheck.cpp
StringLiteralIterator.cpp
TestFilenameCheck.cpp
TextStyleCheck.cpp
TranslatorCommentsCheck.cpp
UseNamedPointConstantsCheck.cpp
Expand Down
2 changes: 2 additions & 0 deletions tools/clang-tidy-plugin/CataTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "NoStaticGettextCheck.h"
#include "PointInitializationCheck.h"
#include "SimplifyPointConstructorsCheck.h"
#include "TestFilenameCheck.h"
#include "TextStyleCheck.h"
#include "TranslatorCommentsCheck.h"
#include "UseNamedPointConstantsCheck.h"
Expand All @@ -31,6 +32,7 @@ class CataModule : public ClangTidyModule
CheckFactories.registerCheck<PointInitializationCheck>( "cata-point-initialization" );
CheckFactories.registerCheck<SimplifyPointConstructorsCheck>(
"cata-simplify-point-constructors" );
CheckFactories.registerCheck<TestFilenameCheck>( "cata-test-filename" );
CheckFactories.registerCheck<TextStyleCheck>( "cata-text-style" );
CheckFactories.registerCheck<TranslatorCommentsCheck>( "cata-translator-comments" );
CheckFactories.registerCheck<UseNamedPointConstantsCheck>(
Expand Down
56 changes: 56 additions & 0 deletions tools/clang-tidy-plugin/TestFilenameCheck.cpp
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
26 changes: 26 additions & 0 deletions tools/clang-tidy-plugin/TestFilenameCheck.h
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
6 changes: 6 additions & 0 deletions tools/clang-tidy-plugin/test/test-filename.cpp
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]

0 comments on commit e312278

Please sign in to comment.