[PATCH] D152589: [clang-tidy] Add readability test for not allowing relative includes

Piotr Zegar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 13 12:04:00 PDT 2023


PiotrZSL added a comment.

In D152589#4418649 <https://reviews.llvm.org/D152589#4418649>, @ErezAmihud wrote:

> I want to make sure I understand.
> The ideal situation would be to create `misc-no-relative-includes` check that checks for `..` in paths (meaning - relative paths), and have a `StrictMode` which does the check for angled-bracket includes?
>
> BTW The reason I initially checked for angled-bracket includes is that according to this <https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html> quote includes search in paths relative to the current file, and the angled-bracket includes check only in the defined include directories.
> In this case there is no risk that the include would get a file that is not relative to the include directory the user specified in the compile flags.

But according to paper you can also do `-I<path to .cpp directory>`  and in .cpp you can do `#include <header.hpp>` and if that header.hpp exist near .cpp then this is still relative include.
For me we should do something like this:

  path_to_cpp_directory = absolutepath(dirname(main-file))
  include_file = argument to #include <> or #include ""
  if (exists(path_to_cpp_directory  + include_file))
    if (StrictMode || !absolutepath(path_to_cpp_directory  + include_file).starts_with(path_to_cpp_directory))
         // relative include, generate warning




Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152589



More information about the cfe-commits mailing list