Why is clang-tidy scanning other header files? - c++

I'm running clang-tidy on a header file header.h. However as some of the warning outputs, it's outputting system headers:
.../include/c++/8/bits/std_abs.h:46:8: error: expected identifier or '(' [clang-diagnostic-error]
extern "C++"
../include/c++/8/cctype:62:1: error: unknown type name 'namespace' [clang-diagnostic-error]
namespace std
../include/c++/8/cctype:62:14: error: expected ';' after top level declarator [clang-diagnostic-error]
namespace std
..
etc
The problem: I don't want to see the warnings for anything other than the source file I'm scanning, for either a source file or header file.
I've tried implementing the fix here (What is the correct way of providing header-filter for clang-tidy in Cmake?) using --header-filter but it didnt work. I added the path to the header file that I was scanning in the regex, but I was still seeing the system header warnings.

For clang-tidy to work your code needs to be compile-able by the clang backend to generate an AST. This is apparently not the case since clang-diagnostic-error is basically a compilation error.
The problem is you are including headers that cannot be compiled by clang, there is no way to filter that out.

Related

Clang-tidy not finding header file

Note: This is done in RHEL 7.2 with clang 8.0.1.
I'm running the command
clang-tidy test.C -- -I/path/to/header.h
and I get the following error:
1 error generated.
Error while processing test.C.
test.C:28:10: error: 'header.h' file not found [clang-diagnostic-error]
#include "header.h"
^
Found compiler error(s).
When I build using g++, it works fine. The above include is the first include statement of the file, and there's some more after it. The file structure is complicated, so it'd be difficult to show where the files are located. I thought the -I argument would find the header, but it doesn't. So, how do I get clang-tidy to find those headers?
Clang tools usually need a compilation database : the compile_commands.json.
It provides information required to build your cpp units (g.e the include directories).
You can generate this file from Make, CMake, etc. by using some external tools.
https://github.com/rizsotto/Bear is one of them.

protoc generated header file has errors

I'm getting strange outputs when I compile my *.proto file using the protoc compiler.
The *.pb.h and *.pb.cc files compile just fine, but the *.pb.h is riddled with syntax errors:
inline specifier allowed on function declarations only
expected a ')'
'struct': missing tag name
And so on...
I'm not sure how to resolve this problem. If there was something wrong with my *.proto file, I would get a compilation error I think? How do I begin debugging this?

How to search for header files in usr/include/linux

I am writing a C++ program with cmake in Ubuntu, which uses the header file joystick.h in /usr/include/linux. By default, it does not seem that make can find joystick.h in the default directories if I use #include <joystick.h>. Therefore, I add the line include_directories (/usr/include/linux) to CMakeLists.txt to enable this header to be found.
The problem is, I now get all sorts of errors such as error: redefinition of ‘struct timeval’ and error: redefinition of ‘struct timezone’. This seems to be because there is a header time.h in both /user/include and /usr/include/linux. The header guards in these files are #ifndef _TIME_H and #ifndef _LINUX_TIME_H respectively. Therefore, the only solution I can think of is to hardcode the path to joystick.h in my source code.
Because of this, it seems to me that /usr/include/linux should never be added to the search path due to these redefinition issues. Is this correct? And is my solution of hardcoding the path the best one?
Unless there is a reason not to (and there might be I suppose) it would seem that just using #include <linux/joystick.h> in the source file would be the simplest solution here.

How to fix header file error?

So, my question is how to fix some error in header file, to run program normally? For example I use c++ builder 2010 and when winuser.h file is included, the program always get error like this
Checking project dependencies...
Compiling Project7.cbproj (Debug
configuration) [BCC32 Error]
winuser.h(47): E2257 , expected Full
parser context
File6.cpp(4): #include c:\program files (x86)\embarcadero\rad
studio\7.0\include\winuser.h [BCC32
Error] winuser.h(48): E2257 , expected
Full parser context
i try to replace that file with original from default installation, but that still get same error, how to fix that?
The error is almost certainly caused by whatever code appears before line 4 of File6.cpp. Most likely that is another header file, in which case it is likely that the code therein is malformed - a missing semicolon or brace for example.
The quickest way to verify that winuser.h is not the issue is to change the order of inclusion so that winuser.h is included first.
Another possibility is that something in winuser.h is dependent on some other header not previously included or directly included in winuser.h. Most Win32 API headers are included by windows.h, and it is generally advisable to include windows,h rather than either of its children.
The message is hard to read but the actual error is "E2257 , expected" (coma expected)
From the RAD studio documentation:
A comma was expected in a list of declarations, initializations, or parameters.
This problem is often caused by a missing syntax element earlier in the file
or one of its included headers.
The error message give you the line where it happened and you should probably look before that. There is probably some '}', ')' or ';' or other syntaxic closer missing in your code just before the error (likely before inclusion of the header file in your code). Full error message (you truncated it) or actual code would make it easier to spot.
It is also possible, even if unlikely, that the error is in one of the headers included in winuser.h.

Visual Studio 2008, error c2039: 'set_new_handler' : is not a member of 'std'

So the other day I went to compile a VC++ project I am working on and all of a sudden I get errors in almost all of my files saying:
new.h: error C2039: 'set_new_handler' : is not a member of 'std
new.h: error C2039: 'set_new_handelr' : symbol cannot be used in a using-declaration
"new.h" and 'set_new_handler' are not being used in any of my files, so I have no idea how or why these errors are suddenly appearing since they relate to a windows/VS library file.
Would anyone know what I can do to clear this error and compile my code again?
UPDATE After examining the files being included upon compilation, some files are including and some are . The problem is that is being included in afxwin.h and is being included in a third-party library. I honestly have no idea what to do with this problem...no other developers that have played with this code are running into this problem, may it be a settings problem? I am not using precompiled headers.
If I were to hazard a guess, I would say that <new.h> declares set_new_handler in the global namespace and <new> declares it within the std namespace. Some code is including <new.h> and expecting it to act as if it had included <new>. I would suspect either some 3rd party library/header or a precompiled header as suggested by Evan.
You can narrow down the culprit using either /showIncludes or pre-processing a source code file (using /E) and examining the output. I usually use the latter and look at the #line directives in the output file to figure out the include chain.
Good luck.