Issue 166151
Summary [clang-tidy] How to mute diagnostic from thirdparty lib header (header only lib)
Labels clang-tidy
Assignees
Reporter codelimit
    Hi!
### The issue:
I have C++ project with some external dependencies (thirdparty libs). These thidrparty libs have the same filesystem root with project sources:
```
root
    - core
        - src
            ...
        - modules
            - audio
                - src
                - thirdpary_lib
                    -include
                    -lib
 ...
    - client
        ... 
    - server
        ...
``` 
The problem is that clang-tidy reports warnings from external libs that I would like to skip. This mostly relates to header-only libraries, or any other headers that contains methods' declarations (inline methods for example, but not necessary).
I understand that there is a way to contact the maintainers to fix or mute the issue in their code, but sometimes it is difficult or impossible for various reasons.
The other way is to fix/mute myself, which I also don't like.

I was hoping that if I pass
`clang-tidy --header-filter="path/to/project/root/.*" src.cpp -- -isystem core/modules/audio/thirdparty_lib/include`
 clang-tidy will ignore the issue in thirdparty lib. However this didn't happen.

I also tried
`clang-tidy src.cpp --header-filter="path/to/project/root/.*" --exclude-header-filter=".*thirdparty_lib.*"`,
but this didn't help also.

And finally I tried to put .clang-tidy with `Checks: -*` in thirdparty_lib directory - no effect also.

So as I understood there only one way to mute this: to place NOLINTNEXTLINE in each place where my code calls thirdparty lib's code.

Question 1: Why `-isystem` and `--exclude-header-filter` didn't work? The issue is in
Question 2: Is there any other way to mute the warning from thirdparty's lib header? Preferably in one place.

---
### Minimal repro example:
[tidy.zip](https://github.com/user-attachments/files/23301045/tidy.zip)

Consider the following project:
**main.cpp**
```cpp
#include <lib.h>
int main()
{
    tidy::entry* e = tidy::open();
    tidy::close(e);
 return 0;
}
```
**header_only_libs/lib.h**
```cpp
#pragma once
#include <malloc.h>
namespace tidy
{
    struct entry
    {};

    entry* open()
    {
        int* ptr = (int*)malloc(5 * sizeof(int));
 return nullptr;
    }

    void close(entry* e)
    {}
}
```
invoke clang-tidy:
`clang-tidy /full/path/to/main.cpp --header-filter=.* -- -isystem /full/path/to/header_only_libs`
outputs:
```
error: Potential leak of memory pointed to by 'ptr' [clang-analyzer-unix.Malloc,-warnings-as-errors]
   14 |         return nullptr;
      |         ^
e:\work\repo\tidy3\main.cpp:5:22: note: Calling 'open'
    5 |     tidy::entry* e = tidy::open();
      | ^~~~~~~~~~~~
e:/work/repo/tidy3/header_only_libs\lib.h:13:26: note: Memory is allocated
   13 |         int* ptr = (int*)malloc(5 * sizeof(int));
      | ^~~~~~~~~~~~~~~~~~~~~~~
e:/work/repo/tidy3/header_only_libs\lib.h:14:9: note: Potential leak of memory pointed to by 'ptr'
   14 |         return nullptr;
      |         ^
Suppressed 4884 warnings (4884 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
1 warning treated as error
```
---

Any hints are highly appreciated.
Best regards.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to