1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
//===--- IncludeCleanerTest.cpp - clang-tidy -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ClangTidyDiagnosticConsumer.h"
#include "ClangTidyOptions.h"
#include "ClangTidyTest.h"
#include "misc/IncludeCleanerCheck.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
#include "gtest/gtest.h"
#include <initializer_list>
#include <optional>
#include <vector>
using namespace clang::tidy::misc;
namespace clang {
namespace tidy {
namespace test {
namespace {
std::string
appendPathFileSystemIndependent(std::initializer_list<std::string> Segments) {
llvm::SmallString<32> Result;
for (const auto &Segment : Segments)
llvm::sys::path::append(Result, llvm::sys::path::Style::native, Segment);
return std::string(Result.str());
}
TEST(IncludeCleanerCheckTest, BasicUnusedIncludes) {
const char *PreCode = R"(
#include "bar.h"
#include <vector>
#include "bar.h"
)";
const char *PostCode = "\n";
std::vector<ClangTidyError> Errors;
EXPECT_EQ(PostCode, runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt,
ClangTidyOptions(), {{"bar.h", ""}, {"vector", ""}}));
}
TEST(IncludeCleanerCheckTest, SuppressUnusedIncludes) {
const char *PreCode = R"(
#include "bar.h"
#include "foo/qux.h"
#include "baz/qux/qux.h"
#include <vector>
)";
const char *PostCode = R"(
#include "bar.h"
#include "foo/qux.h"
#include <vector>
)";
std::vector<ClangTidyError> Errors;
ClangTidyOptions Opts;
Opts.CheckOptions["IgnoreHeaders"] = llvm::StringRef{llvm::formatv(
"bar.h;{0};{1};vector",
llvm::Regex::escape(appendPathFileSystemIndependent({"foo", "qux.h"})),
llvm::Regex::escape(appendPathFileSystemIndependent({"baz", "qux"})))};
EXPECT_EQ(
PostCode,
runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, Opts,
{{"bar.h", ""},
{"vector", ""},
{appendPathFileSystemIndependent({"foo", "qux.h"}), ""},
{appendPathFileSystemIndependent({"baz", "qux", "qux.h"}), ""}}));
}
TEST(IncludeCleanerCheckTest, BasicMissingIncludes) {
const char *PreCode = R"(
#include "bar.h"
int BarResult = bar();
int BazResult = baz();
)";
const char *PostCode = R"(
#include "bar.h"
#include "baz.h"
int BarResult = bar();
int BazResult = baz();
)";
std::vector<ClangTidyError> Errors;
EXPECT_EQ(PostCode,
runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, ClangTidyOptions(),
{{"bar.h", R"(#pragma once
#include "baz.h"
int bar();
)"},
{"baz.h", R"(#pragma once
int baz();
)"}}));
}
TEST(IncludeCleanerCheckTest, SuppressMissingIncludes) {
const char *PreCode = R"(
#include "bar.h"
int BarResult = bar();
int BazResult = baz();
int QuxResult = qux();
)";
ClangTidyOptions Opts;
Opts.CheckOptions["IgnoreHeaders"] = llvm::StringRef{
"baz.h;" +
llvm::Regex::escape(appendPathFileSystemIndependent({"foo", "qux.h"}))};
std::vector<ClangTidyError> Errors;
EXPECT_EQ(PreCode, runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, Opts,
{{"bar.h", R"(#pragma once
#include "baz.h"
#include "foo/qux.h"
int bar();
)"},
{"baz.h", R"(#pragma once
int baz();
)"},
{appendPathFileSystemIndependent({"foo", "qux.h"}),
R"(#pragma once
int qux();
)"}}));
}
TEST(IncludeCleanerCheckTest, SystemMissingIncludes) {
const char *PreCode = R"(
#include <vector>
std::string HelloString;
std::vector Vec;
)";
const char *PostCode = R"(
#include <string>
#include <vector>
std::string HelloString;
std::vector Vec;
)";
std::vector<ClangTidyError> Errors;
EXPECT_EQ(PostCode,
runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, ClangTidyOptions(),
{{"string", R"(#pragma once
namespace std { class string {}; }
)"},
{"vector", R"(#pragma once
#include <string>
namespace std { class vector {}; }
)"}}));
}
TEST(IncludeCleanerCheckTest, PragmaMissingIncludes) {
const char *PreCode = R"(
#include "bar.h"
int BarResult = bar();
int FooBarResult = foobar();
)";
const char *PostCode = R"(
#include "bar.h"
#include "public.h"
int BarResult = bar();
int FooBarResult = foobar();
)";
std::vector<ClangTidyError> Errors;
EXPECT_EQ(PostCode,
runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, ClangTidyOptions(),
{{"bar.h", R"(#pragma once
#include "private.h"
int bar();
)"},
{"private.h", R"(#pragma once
// IWYU pragma: private, include "public.h"
int foobar();
)"}}));
}
TEST(IncludeCleanerCheckTest, DeclFromMacroExpansion) {
const char *PreCode = R"(
#include "foo.h"
DECLARE(myfunc) {
int a;
}
)";
std::vector<ClangTidyError> Errors;
EXPECT_EQ(PreCode,
runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, ClangTidyOptions(),
{{"foo.h",
R"(#pragma once
#define DECLARE(X) void X()
)"}}));
PreCode = R"(
#include "foo.h"
DECLARE {
int a;
}
)";
EXPECT_EQ(PreCode,
runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, ClangTidyOptions(),
{{"foo.h",
R"(#pragma once
#define DECLARE void myfunc()
)"}}));
}
} // namespace
} // namespace test
} // namespace tidy
} // namespace clang
|