Code duplication over multiple config headers

Hello,

parts of our codebase is constructed to have config files which define class members in a specific header file for each variant.

E.g. in Class.cpp:

#if defined(VARIANT_1)
#include "Config_Variant_1.h"
#elif defined(VARIANT_2)
#include "Config_Variant_2.h"
#endif

In Config_Variant_1.h

#ifndef CONF_VAR_1
#define CONF_VAR_1

#if defined(VARIANT_1)

uint8_t Class::MemberVar = 1
// ... More config ...

#endif // VARIANT_1
#endif // CONF_VAR_1

Config_Variant_2.h looks very but has some variables / array entries changed. SonarQube not detects a lot of code duplications since we have multiple Variants who have a lot of configuration in common. Putting everything in one file and only separating changes behind defines is no option.
What can I do to get rid of all the code duplication code smells? I already tried checking for global define with preprocessor twice (when including file and in config file itself). And why is it in first place allowed to scan files that are locked by a define that is never set in first place?
We build with scons build wrapper in Azure pipeline and scons has properly defined variants and global defines.

Thank you
Marius

Hello,

You could try to exclude those files from duplication detection.

Please, let me know if this helps.

Hello,
how can I do that?
We currently use this setup:

sonar.sources=.
sonar.inclusions=src/**/*

There are 2 issues I see:

  1. Since we have a lot of SDK for reference functions and we only want to scan our source we only include src/** for SQ. Can we actually exclude something we exclude? So lets say:
sonar.exclusions=**Variant_**
  1. When building VARIANT_1 I still want to have the Config_Variant_1.h checked but not the Config_Variant_2.h. (How) can I make the exclusion based on the variant I am scanning?

Thank you

Edit: I just found the “Exclusion for duplication detection”! I will look into that. Maybe that helps for this exact issue. But originally we also enable / deactivate whole modules with global defines which is also not registered by SQ. Independent from duplication

Have you tried Code Variants? However, it is not available in the latest LTA (9.9)