1 //===--- IncludeSpellerTest.cpp--------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang-include-cleaner/IncludeSpeller.h"
10 #include "clang-include-cleaner/Analysis.h"
11 #include "clang-include-cleaner/Types.h"
12 #include "clang/Lex/Preprocessor.h"
13 #include "clang/Testing/TestAST.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Path.h"
17 #include "gtest/gtest.h"
20 namespace clang::include_cleaner
{
23 const char *testRoot() {
25 return "C:\\include-cleaner-test";
27 return "/include-cleaner-test";
31 std::string
testPath(llvm::StringRef File
) {
32 assert(llvm::sys::path::is_relative(File
) && "FileName should be relative");
34 llvm::SmallString
<32> NativeFile
= File
;
35 llvm::sys::path::native(NativeFile
, llvm::sys::path::Style::native
);
36 llvm::SmallString
<32> Path
;
37 llvm::sys::path::append(Path
, llvm::sys::path::Style::native
, testRoot(),
39 return std::string(Path
.str());
42 class DummyIncludeSpeller
: public IncludeSpeller
{
44 std::string
operator()(const IncludeSpeller::Input
&Input
) const override
{
45 llvm::StringRef AbsolutePath
= Input
.H
.physical()->tryGetRealPathName();
46 std::string RootWithSeparator
{testRoot()};
47 RootWithSeparator
+= llvm::sys::path::get_separator();
48 if (!AbsolutePath
.consume_front(llvm::StringRef
{RootWithSeparator
}))
50 return "\"" + AbsolutePath
.str() + "\"";
54 TEST(IncludeSpeller
, IsRelativeToTestRoot
) {
57 Inputs
.ExtraArgs
.push_back("-isystemdir");
59 Inputs
.ExtraFiles
[testPath("foo.h")] = "";
60 Inputs
.ExtraFiles
["dir/header.h"] = "";
63 auto &FM
= AST
.fileManager();
64 auto &HS
= AST
.preprocessor().getHeaderSearchInfo();
65 const auto *MainFile
= AST
.sourceManager().getFileEntryForID(
66 AST
.sourceManager().getMainFileID());
68 EXPECT_EQ("\"foo.h\"", spellHeader({Header
{*FM
.getFile(testPath("foo.h"))},
70 EXPECT_EQ("<header.h>",
71 spellHeader({Header
{*FM
.getFile("dir/header.h")}, HS
, MainFile
}));
74 IncludeSpellingStrategy::Add
<DummyIncludeSpeller
>
75 Speller("dummy", "Dummy Include Speller");
78 } // namespace clang::include_cleaner