[PATCH] Add readability-redundant-include check to clang-tidy

Richard legalize at xmission.com
Sat Feb 28 23:06:05 PST 2015


Hi alexfh,

This check looks for redundant header file inclusion.

Every time a `#include` is processed in the main file, it checks a vector of filenames to see if the included file has already been included.  If so, it issues a warning and a replacement to remove the entire line containing the duplicated include directive.

When a macro is defined or undefined in the main file, the vector of filenames is cleared.  This enables the following use case:

```
class LangOptionsBase {
public:
  // Define simple language options (with no accessors).
#define LANGOPT(Name, Bits, Default, Description) unsigned Name : Bits;
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description)
#include "clang/Basic/LangOptions.def"

protected:
  // Define language options of enumeration type. These are private, and will
  // have accessors (below).
#define LANGOPT(Name, Bits, Default, Description)
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
  unsigned Name : Bits;
#include "clang/Basic/LangOptions.def"
};
```

Since macros are redefined between the inclusion of `LangOptions.def`, they are not flagged as redundant.

http://reviews.llvm.org/D7982

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/ReadabilityTidyModule.cpp
  clang-tidy/readability/RedundantInclude.cpp
  clang-tidy/readability/RedundantInclude.h
  test/clang-tidy/readability-redundant-include.cpp
  test/clang-tidy/readability-redundant-include.h
  test/clang-tidy/types.h

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7982.20952.patch
Type: text/x-patch
Size: 9172 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150301/fda5411c/attachment.bin>


More information about the cfe-commits mailing list