devops.cpp.ast.checks.macro_replacement
Flag banned macro invocations and suggest their replacement.
Any macro invocation whose name matches a configured key in
macro_to_replacement is reported, naming the replacement macro that
should be used instead. Built for cases like disallowing gtest’s
EXPECT_THROW/ASSERT_THROW in favor of custom
EXPECT_THROW_MSG/ASSERT_THROW_MSG macros that also require a
failure message, but works for any macro pair.
Detection is based on libclang’s MACRO_INSTANTIATION cursor (available
because the AST engine always parses with
PARSE_DETAILED_PROCESSING_RECORD), so it works regardless of whether the
macro is defined in the same file or an included header, and it matches the
macro name exactly — EXPECT_THROW never accidentally matches
EXPECT_THROW_MSG.
Ships with a built-in default mapping and requires no configuration to use:
EXPECT_THROW -> EXPECT_THROW_MSG
ASSERT_THROW -> ASSERT_THROW_MSG
Replace or extend it via:
[cpp.ast_check_config.macroReplacement]
macro_to_replacement = { EXPECT_THROW = "EXPECT_THROW_MSG",
ASSERT_THROW = "ASSERT_THROW_MSG" }
To disable this check for a project set:
[cpp]
ast_check_disabled_ids = ["macroReplacement"]
Classes
Flag invocations of a banned macro and suggest its replacement. |
- class devops.cpp.ast.checks.macro_replacement.MacroReplacement[source]
Bases:
CheckFlag invocations of a banned macro and suggest its replacement.
- configure(config)[source]
Replace the default macro-to-replacement mapping from the TOML config.
- Parameters:
config (dict) – Expected shape:
{"macro_to_replacement": {"OLD_MACRO": "NEW_MACRO", ...}}. Whenmacro_to_replacementis absent, the built-in default mapping is kept unchanged.- Raises:
ConfigError – If
macro_to_replacementis present but has an invalid shape.- Return type:
None
- visit(cursor, filename)[source]
Flag the cursor if it is an invocation of a banned macro.
- Parameters:
cursor (clang.Cursor) – The AST node currently being visited.
filename (str) – Path of the file being checked.
- Returns:
list[Diagnostic] – A single diagnostic if cursor is a banned macro invocation, otherwise an empty list.
- Return type: