[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / include-cleaner / unittests / IncludeSpellerTest.cpp
blob1dd4d28ed81be772718b70c9f4f9f6184a116a4f
1 //===--- IncludeSpellerTest.cpp--------------------------------------------===//
2 //
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
6 //
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"
18 #include <assert.h>
19 #include <string>
20 namespace clang::include_cleaner {
21 namespace {
23 const char *testRoot() {
24 #ifdef _WIN32
25 return "C:\\include-cleaner-test";
26 #else
27 return "/include-cleaner-test";
28 #endif
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(),
38 NativeFile);
39 return std::string(Path.str());
42 class DummyIncludeSpeller : public IncludeSpeller {
43 public:
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}))
49 return "";
50 return "\"" + AbsolutePath.str() + "\"";
54 TEST(IncludeSpeller, IsRelativeToTestRoot) {
55 TestInputs Inputs;
57 Inputs.ExtraArgs.push_back("-isystemdir");
59 Inputs.ExtraFiles[testPath("foo.h")] = "";
60 Inputs.ExtraFiles["dir/header.h"] = "";
61 TestAST AST{Inputs};
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"))},
69 HS, MainFile}));
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");
77 } // namespace
78 } // namespace clang::include_cleaner