# C++ duplication detection

**URL:** <https://community.sonarsource.com/t/c-duplication-detection/190552>\
**Category:** SonarQube Server / Community Build\
**Tags:** cfamily\
**Created:** [October 27, 2026, 12:05pm UTC](https://community.sonarsource.com/t/c-duplication-detection/190552 "2026-10-27T12:05:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KUGA2](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/kuga2/32/18627_2.png) [@KUGA2](https://community.sonarsource.com/u/KUGA2)\
**Post date:** [October 27, 2026, 12:05pm UTC](https://community.sonarsource.com/t/c-duplication-detection/190552/1 "2026-10-27T12:05:28Z")

</div>

Environment:

- Product: SonarQube Server
- Version: 2025.4.4.119049
- Language: C++
- Analysis type: Pull request analysis
- Metric: new\_duplicated\_lines\_density

Problem description:  
SonarQube reports duplicated lines between two implementations of a  
C++ interface class

The duplicated sections consist of interface/implementation boilerplate and  
Doxygen documentation references. We already use `\copydoc` in the  
implementation in order to avoid duplicating the documentation text. From a  
C++ design perspective, the repeated declarations and `override` methods are  
required by the interface/implementation relationship and cannot be factored  
out without changing the API or reducing readability.

The duplication is therefore not accidental copy-paste. It is caused by a  
common and unavoidable C++ pattern: an interface declares the virtual methods,  
while the implementation has to repeat the corresponding method declarations  
and definitions.

Expected behavior:  
The duplication detector should avoid reporting or significantly reduce  
duplication for unavoidable C++ interface/implementation constructs, especially  
when the implementation uses Doxygen `\copydoc` instead of repeating the full  
documentation.

Alternatively, SonarQube should provide a documented way to exclude such  
constructs from duplication detection without excluding the complete files.

Actual behavior:  
The pull request receives duplicated-code findings or an increased  
`new_duplicated_lines_density` value for these interface/implementation  
constructs.

Impact:  
This produces a misleading duplication result and can fail the quality gate,  
although the code follows a standard C++ design pattern and the documentation  
has already been deduplicated using `\copydoc`.

Reproducer:  
The following reduced pattern reproduces the situation:

```cpp
class IExample
{
public:
    virtual ~IExample() = default;

    //! Performs the operation.
	//!
    virtual void perform() = 0;
};

class ExampleA final : public IExample
{
public:
    ~ExampleA() override = default;

    //! \copydoc IExample::perform()
	//!
    void perform() override;
};

class ExampleB final : public IExample
{
public:
    ~ExampleB() override = default;

	// THE FOLLOWING COMMENT IS MARKED AS DUPLICATION	
    //! \copydoc IExample::perform()
	//!
    void perform() override;
};

```

---

<div class="post-metadata">

**Author:** ![andres](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/andres/32/76342_2.png) [@andres](https://community.sonarsource.com/u/andres)\
**Post date:** [October 30, 2026, 4:10pm UTC](https://community.sonarsource.com/t/c-duplication-detection/190552/2 "2026-10-30T16:10:31Z")

</div>

Hi @KUGA2, let’s keep it to [one problem per post](https://community.sonarsource.com/guidelines#one-subject). Can I suggest we investigate the C++ duplication detection here. and that you create another post giving us some more details about the PR decoration problem for duplicated code?

I’m flagging this for the team responsible for duplication detection so that they can consider whether it makes sense to detect this code as duplicate or not. Thanks for reporting!

---

<div class="post-metadata">

**Author:** ![KUGA2](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/kuga2/32/18627_2.png) [@KUGA2](https://community.sonarsource.com/u/KUGA2)\
**Post date:** [November 4, 2026, 9:03am UTC](https://community.sonarsource.com/t/c-duplication-detection/190552/4 "2026-11-04T09:03:49Z")

</div>

If you insist.. :(. This is now only about the SECOND issue. Edited OP.  
Looking forward to a solution.

---

<div class="post-metadata">

**Author:** ![JolyLoic](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/jolyloic/32/199_2.png) [@JolyLoic](https://community.sonarsource.com/u/JolyLoic)\
**Post date:** [November 9, 2026, 10:07am UTC](https://community.sonarsource.com/t/c-duplication-detection/190552/6 "2026-11-09T10:07:20Z")

</div>

Hi @KUGA2,

I agree with you that ideally, this code should not be marked as duplicated. Unfortunately, the algorithm we use to detect duplicated code works at low level and has no notion of classes and override.

I can’t think of a clean way to solve this issue. I created a ticket to track this and see if we can think of a clever trick that could work.

---

<div class="post-metadata">

**Author:** ![KUGA2](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/kuga2/32/18627_2.png) [@KUGA2](https://community.sonarsource.com/u/KUGA2)\
**Post date:** [November 9, 2026, 11:43am UTC](https://community.sonarsource.com/t/c-duplication-detection/190552/8 "2026-11-09T11:43:50Z")

</div>

> [@JolyLoic](#):
>
> use to detect duplicated code works at low level and has no notion of classes and override.

I do not think that is correct. It does know of overrides (and their necessity to repeat code). If it would not have any special handling for that, it would also mark the C++ code as duplicate. But it only marks the comments! **I repeat: The code is not marked as duplication (good), only the comment!**

> [@JolyLoic](#):
>
> I can’t think of a clean way to solve this issue. I created a ticket to track this and see if we can think of a clever trick that could work.

I can :D. Treat all lines with doxygen copydoc (or similar) as no duplication.

---

<div class="post-metadata">

**Author:** ![JolyLoic](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/jolyloic/32/199_2.png) [@JolyLoic](https://community.sonarsource.com/u/JolyLoic)\
**Post date:** [November 9, 2026, 3:13pm UTC](https://community.sonarsource.com/t/c-duplication-detection/190552/9 "2026-11-09T15:13:28Z")

</div>

> [@KUGA2](#):
>
> **I repeat: The code is not marked as duplication (good), only the comment!**

That sounds strange… Comments are supposedly fully ignored by the algorithm… Can you share a screenshot and a reproducer file?

---

<div class="post-metadata">

**Author:** ![KUGA2](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/kuga2/32/18627_2.png) [@KUGA2](https://community.sonarsource.com/u/KUGA2)\
**Post date:** [November 9, 2026, 4:19pm UTC](https://community.sonarsource.com/t/c-duplication-detection/190552/10 "2026-11-09T16:19:05Z")

</div>

You were right. Duplication appears on all lines.

This is how it looks like in PR:

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/8/a/8aef51c9a53c00acf5b9109a941b1ee46ff52809.png)

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/9/2/92398a63809c4b8c7af7517d6c2de0801e23a689.png)

In my reproduction, i only added those comments. The duplicate code itself already there.

I can see that in main branch:

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/6/b/6bd3f378f53b3c43d23d9ff0eff9804076ed6242.png)

> [@JolyLoic](#):
>
> I can’t think of a clean way to solve this issue. I created a ticket to track this and see if we can think of a clever trick that could work.

Updated suggestion Ignore lines (or blocks) with:

1. override;
2. final;
3. \copydoc
