Skip to content

Commit

Permalink
Enable the misc-use-anonymous-namespace clang-tidy check (#7661)
Browse files Browse the repository at this point in the history
* Upgrade clang-format and clang-tidy to use v16

(Skipping over 15 entirely in favor of the newest stable version)

* Update presubmit.yml

* Update .clang-tidy

* Update .clang-tidy

* fixes

* Update run-clang-tidy.sh

* Update .clang-tidy

* Update .clang-tidy

* fixes

* Update .clang-tidy

* Update PyHalide.cpp

* Update run-clang-tidy.sh

* Update CodeGen_Vulkan_Dev.cpp

* Enable the misc-use-anonymous-namespace clang-tidy check

Basically just says "don't use static"

* Update Generator.h

* Update Util.cpp

* Update JITModule.cpp
  • Loading branch information
steven-johnson committed Jun 24, 2023
1 parent c2e4f6d commit 1e3431c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Checks: >
misc-unused-alias-decls,
-misc-unused-parameters,
misc-unused-using-decls,
-misc-use-anonymous-namespace,
misc-use-anonymous-namespace,
-modernize-avoid-bind,
-modernize-avoid-c-arrays,
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGen_D3D12Compute_Dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ using std::sort;
using std::string;
using std::vector;

static ostringstream nil;

namespace {

ostringstream nil;

class CodeGen_D3D12Compute_Dev : public CodeGen_GPU_Dev {
public:
CodeGen_D3D12Compute_Dev(const Target &target);
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGen_Metal_Dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ using std::sort;
using std::string;
using std::vector;

static ostringstream nil;

namespace {

ostringstream nil;

class CodeGen_Metal_Dev : public CodeGen_GPU_Dev {
public:
CodeGen_Metal_Dev(const Target &target);
Expand Down
64 changes: 34 additions & 30 deletions src/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3977,19 +3977,21 @@ namespace halide_register_generator {
struct halide_global_ns;
};

#define _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
namespace halide_register_generator { \
struct halide_global_ns; \
namespace GEN_REGISTRY_NAME##_ns { \
std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
using GenType = std::remove_pointer<decltype(new GEN_CLASS_NAME)>::type; /* NOLINT(bugprone-macro-parentheses) */ \
return GenType::create(context, #GEN_REGISTRY_NAME, #FULLY_QUALIFIED_STUB_NAME); \
} \
} \
static auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
} \
static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
#define _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
namespace halide_register_generator { \
struct halide_global_ns; \
namespace GEN_REGISTRY_NAME##_ns { \
std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
using GenType = std::remove_pointer<decltype(new GEN_CLASS_NAME)>::type; /* NOLINT(bugprone-macro-parentheses) */ \
return GenType::create(context, #GEN_REGISTRY_NAME, #FULLY_QUALIFIED_STUB_NAME); \
} \
} \
namespace { \
auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
} \
} \
static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
"HALIDE_REGISTER_GENERATOR must be used at global scope");

#define _HALIDE_REGISTER_GENERATOR2(GEN_CLASS_NAME, GEN_REGISTRY_NAME) \
Expand Down Expand Up @@ -4039,23 +4041,25 @@ struct halide_global_ns;
// It is specified as a variadic template argument to allow for the fact that the embedded commas
// would otherwise confuse the preprocessor; since (in this case) all we're going to do is
// pass it thru as-is, this is fine (and even MSVC's 'broken' __VA_ARGS__ should be OK here).
#define HALIDE_REGISTER_GENERATOR_ALIAS(GEN_REGISTRY_NAME, ORIGINAL_REGISTRY_NAME, ...) \
namespace halide_register_generator { \
struct halide_global_ns; \
namespace ORIGINAL_REGISTRY_NAME##_ns { \
std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
} \
namespace GEN_REGISTRY_NAME##_ns { \
std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
auto g = ORIGINAL_REGISTRY_NAME##_ns::factory(context); \
const Halide::GeneratorParamsMap m = __VA_ARGS__; \
g->set_generatorparam_values(m); \
return g; \
} \
} \
static auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
} \
static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
#define HALIDE_REGISTER_GENERATOR_ALIAS(GEN_REGISTRY_NAME, ORIGINAL_REGISTRY_NAME, ...) \
namespace halide_register_generator { \
struct halide_global_ns; \
namespace ORIGINAL_REGISTRY_NAME##_ns { \
std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
} \
namespace GEN_REGISTRY_NAME##_ns { \
std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
auto g = ORIGINAL_REGISTRY_NAME##_ns::factory(context); \
const Halide::GeneratorParamsMap m = __VA_ARGS__; \
g->set_generatorparam_values(m); \
return g; \
} \
} \
namespace { \
auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
} \
} \
static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
"HALIDE_REGISTER_GENERATOR_ALIAS must be used at global scope");

// The HALIDE_GENERATOR_PYSTUB macro is used to produce "PyStubs" -- i.e., CPython wrappers to let a C++ Generator
Expand Down
6 changes: 5 additions & 1 deletion src/JITModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ JITModule::Symbol JITModule::argv_entrypoint_symbol() const {
return jit_module->argv_entrypoint;
}

static bool module_already_in_graph(const JITModuleContents *start, const JITModuleContents *target, std::set<const JITModuleContents *> &already_seen) {
namespace {

bool module_already_in_graph(const JITModuleContents *start, const JITModuleContents *target, std::set<const JITModuleContents *> &already_seen) {
if (start == target) {
return true;
}
Expand All @@ -537,6 +539,8 @@ static bool module_already_in_graph(const JITModuleContents *start, const JITMod
return false;
}

} // namespace

void JITModule::add_dependency(JITModule &dep) {
std::set<const JITModuleContents *> already_seen;
internal_assert(!module_already_in_graph(dep.jit_module.get(), jit_module.get(), already_seen)) << "JITModule::add_dependency: creating circular dependency graph.\n";
Expand Down
6 changes: 5 additions & 1 deletion src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,11 @@ struct TickStackEntry {
int line;
};

static vector<TickStackEntry> tick_stack;
namespace {

vector<TickStackEntry> tick_stack;

} // namespace

void halide_tic_impl(const char *file, int line) {
string f = file;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ Checks: >
misc-unused-alias-decls,
-misc-unused-parameters,
misc-unused-using-decls,
-misc-use-anonymous-namespace,
misc-use-anonymous-namespace,
...
4 changes: 3 additions & 1 deletion tools/binary2cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// Embeds a binary blob (from stdin) in a C++ source array of unsigned
// chars. Similar to the xxd utility.

static int usage() {
namespace {
int usage() {
fprintf(stderr, "Usage: binary2cpp identifier [-header]\n");
return -1;
}
} // namespace

int main(int argc, const char **argv) {
const char *target = argv[1];
Expand Down

0 comments on commit 1e3431c

Please sign in to comment.