Fix pr31836 on Windows too, and correctly handle repeated separators.
authorNico Weber <thakis@chromium.org>
Fri, 1 May 2020 00:39:19 +0000 (20:39 -0400)
committerNico Weber <thakis@chromium.org>
Fri, 1 May 2020 18:17:01 +0000 (14:17 -0400)
commitb9d50bdff211eb806dce5bc42167a6b9c0cac6e6
tree4d63c9ef0c3a1e4db5c679bbdf8c8e89b4588741
parentb938168aef995f8d9b6695a0dc0ef71c25b71968
Fix pr31836 on Windows too, and correctly handle repeated separators.

The approach in D30000 assumes that the '/' returned by path::begin()
is the first element for absolute paths, but that's not true on
Windows.

Also, on Windows backslashes in include lines often end up escaped
so that there are two of them. Having backslashes in include lines
is undefined behavior in most cases and implementation-defined
behavior in C++20, but since clang treats it as normal repeated
path separators, the diagnostic should too.

Unbreaks -Wnonportable-include-path for absolute paths on Windows,
and unbreaks it on non-Windows in the case of absolute paths with
repeated directory separators.

This affects e.g. the `#include __FILE__` technique if the file
passed to clang has the wrong case for the drive letter. Before:

C:\src\llvm-project>bin\clang-cl.exe c:\src\llvm-project\test.cc
c:\\src\\llvm-project\\test.cc(4,10): warning: non-portable path to file
    '"c\\srccllvm-projectctest.cc.'; specified path differs in case from
    file name on disk [-Wnonportable-include-path]
         ^

Now:

C:\src\llvm-project> out\gn\bin\clang-cl c:\src\llvm-project\test.cc
c:\\src\\llvm-project\\test.cc(4,10): warning: non-portable path to file
    '"C:\\src\\llvm-project\\test.cc"'; specified path differs in case from
    file name on disk [-Wnonportable-include-path]
         ^

Differential Revision: https://reviews.llvm.org/D79223
clang/lib/Lex/PPDirectives.cpp
clang/test/Lexer/case-insensitive-include-ms.c
clang/test/Lexer/case-insensitive-include-pr31836.sh
clang/test/Lexer/case-insensitive-include.c
llvm/include/llvm/Support/Path.h
llvm/unittests/Support/Path.cpp