[PATCH] D59135: Add check for matching HeaderFilter before emitting Diagnostic

Alexander Kornienko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 8 08:06:51 PDT 2019


alexfh added a comment.

In D59135#1458265 <https://reviews.llvm.org/D59135#1458265>, @thorsten-klein wrote:

> Hello @alexfh ,
>  Let me extend your example
>
>   $ cat a.cc 
>   #include "b.h"
>   #include "d.h"
>   int main(){check(nullptr);}
>   $ cat b.h 
>   #include "c.h"
>   inline void b() { c(/*y=*/42); }
>   $ cat c.h 
>   void c(int x);
>   $ cat d.h 
>   inline char* check(char* buffer)
>   {
>   	*buffer++=1; // Should be clang-analyzer-core.NullDereference
>   	return buffer;
>   }
>  
>
>
> Now an additional warning is found and shown (=not suppressed):
>
>   $ clang-tidy -checks=-*,clang-*,bugprone-argument-comment a.cc --
>   2 warnings generated.
>   /home/default/Temp/clang-tidy-test/d.h:3:11: warning: Dereference of null pointer [clang-analyzer-core.NullDereference]
>           *buffer++=1; // Should be clang-analyzer-core.NullDereference
>                    ^
>   /home/default/Temp/clang-tidy-test/a.cc:3:12: note: Calling 'check'
>   int main(){check(0);}
>              ^
>   /home/default/Temp/clang-tidy-test/d.h:3:3: note: Null pointer value stored to 'buffer'
>           *buffer++=1; // Should be clang-analyzer-core.NullDereference
>            ^
>   /home/default/Temp/clang-tidy-test/d.h:3:11: note: Dereference of null pointer
>           *buffer++=1; // Should be clang-analyzer-core.NullDereference
>                    ^
>   Suppressed 1 warnings (1 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.
>


The behavior in the case above seems to be correct. The null pointer dereference is happening in d.h:3:11, which is not included into the default header-filter (which is empty == no headers are considered "interesting" on their own). However, the (possible) cause of the problem is in a.cc:3:12, which is considered "interesting". Thus the whole diagnostic with all notes attached to it is displayed to the user.

The general rule is: a warning (together with all of its notes) is displayed if its location or location of any of it's notes is inside a the main file or a (non-system, unless -system-headers option is present) header matching the regex configured via the -header-filter option. The -line-filter works in a very similar way, but the whole main file is not whitelisted by default, only the ranges of lines in it that are parts of the -line-filter.

> **How can I use -header-filter now?**

This depends on what you're trying to achieve.

> With -header-filter=b.h clang-tidy shows both warnings:

This aligns well with the logic I described above.

> With -header-filter=c.h clang-tidy shows both warnings:

Same here: the bugprone-argument-comment warning is shown due to the -header-filter=c.h option. The clang-analyzer-core.NullDereference will be shown regardless of the -header-filter value, because it is related to the main file.

> With -header-filter=c.h clang-tidy shows both warnings:

It looks like you wanted to say that with -header-filter=d.h only the clang-analyzer-core.NullDereference warning is shown. Seems correct. See above.

> How can I suppress warning for my header file //**d.h**// so that only warning from //**b.h**// is shown?

The warning in d.h is related to the main file (since it has a note in the main file). It will be displayed regardless of the -header-filter. This is by design.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D59135/new/

https://reviews.llvm.org/D59135





More information about the cfe-commits mailing list