llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.63k stars 11.36k forks source link

Clang could warn about trailing spaces in `#include` filenames #96064

Open zmodem opened 2 months ago

zmodem commented 2 months ago

Consider:

#include "foo.h " // Note: trailing space.

Assuming foo.h exists, the code above will compile on Windows whose filesystem API generally ignores leading and trailing space in filenames. However it will not compile on stricter systems such as Linux.

Maybe Clang should warn? It's niche, but also seems easy to do.

(Yes, we hit this in real code, https://chromium-review.googlesource.com/c/chromium/src/+/5641197)

MitalAshok commented 2 months ago

This also breaks -Wnonportable-include-path:

#include <array>
#include <array >
#include <Array >
#include <Array>
> clang-cl clang-cl test.cpp -fsyntax-only
test.cpp(4,10): warning: non-portable path to file '<array>'; specified path differs in case from file name on disk
      [-Wnonportable-include-path]
    4 | #include <Array>
      |          ^~~~~~~
      |          <array>
1 warning generated.

We should probably extend -Wnonportable-include-path to consider trailing spaces.

Dots at the end are ignored in the same way on Windows too:

// All of these include the same file
#include <Array >
#include <Array.>
#include <Array              >
#include <Array..............>
#include <Array.             >
#include <Array    . . ...  .>

Since these trailing spaces/dots are always removed when you specify a filename (except when prefixed by \\?\), we could issue the warning before even opening the file https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file#naming-conventions (There's probably a more authoritative source on how the file name is transformed, this one only says "Do not end a file or directory name with a space or a period.")

zmodem commented 2 months ago

+1 extending -Wnonportable-include-path for this sounds good!

llvmbot commented 2 months ago

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
  2. In the comments of this issue, request for it to be assigned to you, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot commented 2 months ago

@llvm/issue-subscribers-good-first-issue

Author: Hans (zmodem)

Consider: ``` #include "foo.h " // Note: trailing space. ``` Assuming `foo.h` exists, the code above will compile on Windows whose filesystem API generally ignores leading and trailing space in filenames. However it will not compile on stricter systems such as Linux. Maybe Clang should warn? It's niche, but also seems easy to do. (Yes, we hit this in real code, https://chromium-review.googlesource.com/c/chromium/src/+/5641197)
akshaykumars614 commented 2 months ago

Let me understand the issue. You are saying

include"foo.h " should throw a warning, but it is not?

I am I right? I am new here. Trying to understand things @zmodem

jerryyiransun commented 2 months ago

I would like to work on this issue, can I get assigned please? @shafik