[PATCH] D86597: [Tooling][Format] Treat compound extensions (foo.bar.cc) as matching foo.h
Sam McCall via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 27 06:34:56 PDT 2020
This revision was automatically updated to reflect the committed changes.
Closed by commit rG266825620c7f: [Tooling][Format] Treat compound extensions (foo.bar.cc) as matching foo.h (authored by sammccall).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D86597/new/
https://reviews.llvm.org/D86597
Files:
clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
clang/unittests/Tooling/HeaderIncludesTest.cpp
Index: clang/unittests/Tooling/HeaderIncludesTest.cpp
===================================================================
--- clang/unittests/Tooling/HeaderIncludesTest.cpp
+++ clang/unittests/Tooling/HeaderIncludesTest.cpp
@@ -40,7 +40,7 @@
return *Result;
}
- const std::string FileName = "fix.cpp";
+ std::string FileName = "fix.cpp";
IncludeStyle Style = format::getLLVMStyle().IncludeStyle;
};
@@ -102,6 +102,15 @@
Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
.IncludeStyle;
EXPECT_EQ(Expected, insert(Code, "<a>"));
+
+ FileName = "fix.cu.cpp";
+ EXPECT_EQ(Expected, insert(Code, "<a>"));
+
+ FileName = "fix_test.cu.cpp";
+ EXPECT_EQ(Expected, insert(Code, "<a>"));
+
+ FileName = "bar.cpp";
+ EXPECT_NE(Expected, insert(Code, "<a>")) << "Not main header";
}
TEST_F(HeaderIncludesTest, InsertBeforeSystemHeaderLLVM) {
Index: clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
===================================================================
--- clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
+++ clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
@@ -12,6 +12,7 @@
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/Optional.h"
#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/Path.h"
namespace clang {
namespace tooling {
@@ -174,12 +175,22 @@
const char IncludeRegexPattern[] =
R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";
+// The filename of Path excluding extension.
+// Used to match implementation with headers, this differs from sys::path::stem:
+// - in names with multiple dots (foo.cu.cc) it terminates at the *first*
+// - an empty stem is never returned: /foo/.bar.x => .bar
+// - we don't bother to handle . and .. specially
+StringRef matchingStem(llvm::StringRef Path) {
+ StringRef Name = llvm::sys::path::filename(Path);
+ return Name.substr(0, Name.find('.', 1));
+}
+
} // anonymous namespace
IncludeCategoryManager::IncludeCategoryManager(const IncludeStyle &Style,
StringRef FileName)
: Style(Style), FileName(FileName) {
- FileStem = llvm::sys::path::stem(FileName);
+ FileStem = matchingStem(FileName);
for (const auto &Category : Style.IncludeCategories)
CategoryRegexs.emplace_back(Category.Regex, llvm::Regex::IgnoreCase);
IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||
@@ -222,8 +233,7 @@
bool IncludeCategoryManager::isMainHeader(StringRef IncludeName) const {
if (!IncludeName.startswith("\""))
return false;
- StringRef HeaderStem =
- llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
+ StringRef HeaderStem = matchingStem(IncludeName.drop_front(1).drop_back(1));
if (FileStem.startswith(HeaderStem) ||
FileStem.startswith_lower(HeaderStem)) {
llvm::Regex MainIncludeRegex(HeaderStem.str() + Style.IncludeIsMainRegex,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86597.288308.patch
Type: text/x-patch
Size: 2915 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200827/01176a4a/attachment.bin>
More information about the cfe-commits
mailing list