Skip to content

Commit

Permalink
Ignore classes that were implicitly instantiated from templates (Eric…
Browse files Browse the repository at this point in the history
  • Loading branch information
Seeker04 committed Apr 16, 2024
1 parent a185da4 commit 0bf929a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
4 changes: 3 additions & 1 deletion plugins/cpp/model/include/model/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ enum Tag
Static = 3,
Implicit = 4,
Global = 5,
Constant = 6
Constant = 6,
TemplateSpecialization = 7,
TemplateInstantiation = 8 // all cases other than explicit specialization
};

inline std::string visibilityToString(Visibility v_)
Expand Down
9 changes: 9 additions & 0 deletions plugins/cpp/parser/src/clangastvisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,15 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
cppRecord->isAbstract = crd->isAbstract();
cppRecord->isPOD = crd->isPOD();

if (crd->getTemplateInstantiationPattern())
{
cppRecord->tags.insert(
crd->getTemplateSpecializationKind() == clang::TSK_ExplicitSpecialization
? model::Tag::TemplateSpecialization
: model::Tag::TemplateInstantiation
);
}

//--- CppInheritance ---//

for (auto it = crd->bases_begin(); it != crd->bases_end(); ++it)
Expand Down
7 changes: 7 additions & 0 deletions plugins/cpp_metrics/parser/src/cppmetricsparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ void CppMetricsParser::typeMcCabe()
if (!typeFile || !cc::util::isRootedUnderAnyOf(_inputPaths, typeFile->path))
continue;

// Skip if its a template instantiation
const auto typeEntity = _ctx.db->query_one<Entity>(
odb::query<Entity>::astNodeId == type.id);
if (typeEntity && typeEntity->tags.find(model::Tag::TemplateInstantiation)
!= typeEntity->tags.cend())
continue;

mcValues[type.id] = 0;

// Process its methods
Expand Down
14 changes: 14 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/typemccabe.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,19 @@ class ClassWithInnerClass {
}; // 3
}; // 4

template <typename T>
class TemplateClass {
T value;
public:
T get()
{
return T();
} // 1
void set(T t)
{
value = t;
} // 1
}; // 2

#endif // TYPE_MCCABE__H

14 changes: 14 additions & 0 deletions plugins/cpp_metrics/test/sources/service/typemccabe.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,19 @@ class ClassWithInnerClass {
}; // 3
}; // 4

template <typename T>
class TemplateClass {
T value;
public:
T get()
{
return T();
} // 1
void set(T t)
{
value = t;
} // 1
}; // 2

#endif // TYPE_MCCABE__H

1 change: 1 addition & 0 deletions plugins/cpp_metrics/test/src/cppmetricsparsertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ std::vector<McCabeParam> paramTypeMcCabe = {
{"ClassMethodsInsideAndOutside", 8},
{"ClassWithInnerClass", 4},
{"ClassWithInnerClass::InnerClass", 3},
{"TemplateClass", 2},
};

TEST_P(ParameterizedTypeMcCabeTest, TypeMcCabeTest) {
Expand Down

0 comments on commit 0bf929a

Please sign in to comment.