bruno updated this revision to Diff 149313. bruno added a comment. Updated the patch after Duncan and Aaron reviews. I actually went a bit more aggressive with the fixits, since I realized the conditions for the warning are already strict enough and we should take the chance to be more clear. For the attached testcase, the output now is:
./A.framework/Headers/A.h:2:10: warning: double-quoted include "A0.h" in framework header, expected angle-bracketed include <A/A0.h> instead #include "A0.h" ^~~~~~ <A/A0.h> ./A.framework/Headers/A.h:3:10: warning: double-quoted include "B.h" in framework header, expected angle-bracketed include <B.h> instead #include "B.h" ^~~~~ <B.h> https://reviews.llvm.org/D47157 Files: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticLexKinds.td lib/Lex/HeaderSearch.cpp test/Modules/Inputs/double-quotes/A.framework/Headers/A.h test/Modules/Inputs/double-quotes/A.framework/Headers/A0.h test/Modules/Inputs/double-quotes/A.framework/Modules/module.modulemap test/Modules/Inputs/double-quotes/B.h test/Modules/Inputs/double-quotes/X.framework/Headers/X.h test/Modules/Inputs/double-quotes/X.framework/Modules/module.modulemap test/Modules/Inputs/double-quotes/a.hmap.json test/Modules/Inputs/double-quotes/flat-header-path/Z.h test/Modules/Inputs/double-quotes/flat-header-path/Z.modulemap test/Modules/Inputs/double-quotes/x.hmap.json test/Modules/Inputs/double-quotes/z.yaml test/Modules/double-quotes.m
Index: test/Modules/double-quotes.m =================================================================== --- /dev/null +++ test/Modules/double-quotes.m @@ -0,0 +1,38 @@ +// REQUIRES: shell + +// RUN: rm -rf %t +// RUN: mkdir %t + +// RUN: hmaptool write %S/Inputs/double-quotes/a.hmap.json %t/a.hmap +// RUN: hmaptool write %S/Inputs/double-quotes/x.hmap.json %t/x.hmap + +// RUN: sed -e "s:TEST_DIR:%S/Inputs/double-quotes:g" \ +// RUN: %S/Inputs/double-quotes/z.yaml > %t/z.yaml + +// The output with and without modules should be the same, without modules first. +// RUN: %clang_cc1 \ +// RUN: -I %t/x.hmap -iquote %t/a.hmap -ivfsoverlay %t/z.yaml \ +// RUN: -F%S/Inputs/double-quotes -I%S/Inputs/double-quotes \ +// RUN: -Wquoted-include-in-framework-header -fsyntax-only %s -verify + +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache \ +// RUN: -I %t/x.hmap -iquote %t/a.hmap -ivfsoverlay %t/z.yaml \ +// RUN: -F%S/Inputs/double-quotes -I%S/Inputs/double-quotes \ +// RUN: -Wquoted-include-in-framework-header -fsyntax-only %s \ +// RUN: 2>%t/stderr + +// The same warnings show up when modules is on but -verify doesn't get it +// because they only show up under the module A building context. +// RUN: FileCheck --input-file=%t/stderr %s +// CHECK: double-quoted include "A0.h" in framework header, expected angle-bracketed include <A/A0.h> instead +// CHECK: double-quoted include "B.h" in framework header, expected angle-bracketed include <B.h> instead +// CHECK: double-quoted include "B.h" in framework header, expected angle-bracketed include <B.h> instead + +#import "A.h" +#import <Z/Z.h> + +int bar() { return foo(); } + +// expected-warning@Inputs/double-quotes/A.framework/Headers/A.h:2{{double-quoted include "A0.h" in framework header}} +// expected-warning@Inputs/double-quotes/A.framework/Headers/A.h:3{{double-quoted include "B.h" in framework header}} +// expected-warning@Inputs/double-quotes/flat-header-path/Z.h:1{{double-quoted include "B.h" in framework header}} Index: test/Modules/Inputs/double-quotes/z.yaml =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/z.yaml @@ -0,0 +1,28 @@ +{ + 'version': 0, + 'case-sensitive': 'false', + 'roots': [ + { + 'type': 'directory', + 'name': "TEST_DIR/Z.framework/Headers", + 'contents': [ + { + 'type': 'file', + 'name': "Z.h", + 'external-contents': "TEST_DIR/flat-header-path/Z.h" + } + ] + }, + { + 'type': 'directory', + 'name': "TEST_DIR/Z.framework/Modules", + 'contents': [ + { + 'type': 'file', + 'name': "module.modulemap", + 'external-contents': "TEST_DIR/flat-header-path/Z.modulemap" + } + ] + } + ] +} Index: test/Modules/Inputs/double-quotes/x.hmap.json =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/x.hmap.json @@ -0,0 +1,7 @@ + +{ + "mappings" : + { + "X.h" : "X/X.h" + } +} Index: test/Modules/Inputs/double-quotes/flat-header-path/Z.modulemap =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/flat-header-path/Z.modulemap @@ -0,0 +1,3 @@ +framework module Z { + header "Z.h" +} Index: test/Modules/Inputs/double-quotes/flat-header-path/Z.h =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/flat-header-path/Z.h @@ -0,0 +1 @@ +#import "B.h" Index: test/Modules/Inputs/double-quotes/a.hmap.json =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/a.hmap.json @@ -0,0 +1,7 @@ + +{ + "mappings" : + { + "A.h" : "A/A.h" + } +} Index: test/Modules/Inputs/double-quotes/X.framework/Modules/module.modulemap =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/X.framework/Modules/module.modulemap @@ -0,0 +1,4 @@ + +framework module X { + header "X.h" +} Index: test/Modules/Inputs/double-quotes/X.framework/Headers/X.h =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/X.framework/Headers/X.h @@ -0,0 +1 @@ +// X.h Index: test/Modules/Inputs/double-quotes/B.h =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/B.h @@ -0,0 +1 @@ +// A0.h Index: test/Modules/Inputs/double-quotes/A.framework/Modules/module.modulemap =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/A.framework/Modules/module.modulemap @@ -0,0 +1,5 @@ + +framework module A { + header "A.h" + header "A0.h" +} Index: test/Modules/Inputs/double-quotes/A.framework/Headers/A0.h =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/A.framework/Headers/A0.h @@ -0,0 +1 @@ +// A0.h Index: test/Modules/Inputs/double-quotes/A.framework/Headers/A.h =================================================================== --- /dev/null +++ test/Modules/Inputs/double-quotes/A.framework/Headers/A.h @@ -0,0 +1,7 @@ + +#include "A0.h" +#include "B.h" + +#include "X.h" + +int foo(); Index: lib/Lex/HeaderSearch.cpp =================================================================== --- lib/Lex/HeaderSearch.cpp +++ lib/Lex/HeaderSearch.cpp @@ -621,6 +621,67 @@ return CopyStr; } +static bool isFrameworkStylePath(StringRef Path, + SmallVectorImpl<char> &FrameworkName) { + using namespace llvm::sys; + path::const_iterator I = path::begin(Path); + path::const_iterator E = path::end(Path); + + // Detect different types of framework style paths: + // + // ...Foo.framework/{Headers,PrivateHeaders} + // ...Foo.framework/Versions/{A,Current}/{Headers,PrivateHeaders} + // ...Foo.framework/Frameworks/Nested.framework/{Headers,PrivateHeaders} + // ...<other variations with 'Versions' like in the above path> + // + // and some other variations among these lines. + int FoundComp = 0; + while (I != E) { + if (I->endswith(".framework")) { + FrameworkName.append(I->begin(), I->end()); + ++FoundComp; + } + if (*I == "Headers" || *I == "PrivateHeaders") + ++FoundComp; + ++I; + } + + return FoundComp >= 2; +} + +static bool isFrameworkStylePath(StringRef Path) { + SmallString<128> FrameworkName; + return isFrameworkStylePath(Path, FrameworkName); +} + +static void diagnoseFrameworkInclude(DiagnosticsEngine &Diags, + SourceLocation IncludeLoc, + StringRef Includer, + StringRef IncludeFilename, + const FileEntry *IncludeFE) { + if (!isFrameworkStylePath(Includer)) + return; + + SmallString<128> ToFramework, NewInclude; + StringRef ToFrameworkName; + bool IsIncludeeInFramework = + isFrameworkStylePath(IncludeFE->getName(), ToFramework); + auto D = + Diags.Report(IncludeLoc, diag::warn_quoted_include_in_framework_header); + if (!ToFramework.empty()) + ToFrameworkName = StringRef(ToFramework).drop_back(10); // drop .framework + D << IncludeFilename << IsIncludeeInFramework << ToFrameworkName; + + NewInclude += "<"; + if (IsIncludeeInFramework) { + NewInclude += ToFrameworkName; + NewInclude += "/"; + } + NewInclude += IncludeFilename; + NewInclude += ">"; + D << FixItHint::CreateReplacement(IncludeLoc, NewInclude); +} + /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file, /// return null on failure. isAngled indicates whether the file reference is /// for system \#include's or not (i.e. using <> instead of ""). Includers, if @@ -722,8 +783,12 @@ RelativePath->clear(); RelativePath->append(Filename.begin(), Filename.end()); } - if (First) + if (First) { + diagnoseFrameworkInclude(Diags, IncludeLoc, + IncluderAndDir.second->getName(), Filename, + FE); return FE; + } // Otherwise, we found the path via MSVC header search rules. If // -Wmsvc-include is enabled, we have to keep searching to see if we @@ -834,6 +899,11 @@ return MSFE; } + bool FoundByHeaderMap = !IsMapped ? false : *IsMapped; + if (!Includers.empty() && !isAngled && !FoundByHeaderMap) + diagnoseFrameworkInclude( + Diags, IncludeLoc, Includers.front().second->getName(), Filename, FE); + // Remember this location for the next lookup we do. CacheLookup.HitIdx = i; return FE; Index: include/clang/Basic/DiagnosticLexKinds.td =================================================================== --- include/clang/Basic/DiagnosticLexKinds.td +++ include/clang/Basic/DiagnosticLexKinds.td @@ -709,6 +709,11 @@ def err_mmap_submodule_export_as : Error< "only top-level modules can be re-exported as public">; +def warn_quoted_include_in_framework_header : Warning< + "double-quoted include \"%0\" in framework header, expected angle-bracketed " + "include <%select{|%2/}1%0> instead" + >, InGroup<FrameworkHdrQuotedInclude>, DefaultIgnore; + def warn_auto_module_import : Warning< "treating #%select{include|import|include_next|__include_macros}0 as an " "import of module '%1'">, InGroup<AutoImport>, DefaultIgnore; Index: include/clang/Basic/DiagnosticGroups.td =================================================================== --- include/clang/Basic/DiagnosticGroups.td +++ include/clang/Basic/DiagnosticGroups.td @@ -31,6 +31,7 @@ def Availability : DiagGroup<"availability">; def Section : DiagGroup<"section">; def AutoImport : DiagGroup<"auto-import">; +def FrameworkHdrQuotedInclude : DiagGroup<"quoted-include-in-framework-header">; def CXX14BinaryLiteral : DiagGroup<"c++14-binary-literal">; def CXXPre14CompatBinaryLiteral : DiagGroup<"c++98-c++11-compat-binary-literal">; def GNUBinaryLiteral : DiagGroup<"gnu-binary-literal">;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits