| 1 |
//===----------------------------------------------------------------------===// |
| 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 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "clang-tidy/ClangTidyCheck.h" |
| 10 |
#include "clang-tidy/ClangTidyModuleRegistry.h" |
| 11 |
|
| 12 |
#include "llvm/ADT/ArrayRef.h" |
| 13 |
|
| 14 |
#include "header_exportable_declarations.hpp" |
| 15 |
|
| 16 |
#include <iostream> |
| 17 |
#include <iterator> |
| 18 |
#include <ranges> |
| 19 |
#include <algorithm> |
| 20 |
|
| 21 |
template <> |
| 22 |
struct clang::tidy::OptionEnumMapping<libcpp::header_exportable_declarations::FileType> { |
| 23 |
static llvm::ArrayRef<std::pair<libcpp::header_exportable_declarations::FileType, llvm::StringRef>> getEnumMapping() { |
| 24 |
static constexpr std::pair<libcpp::header_exportable_declarations::FileType, llvm::StringRef> Mapping[] = { |
| 25 |
{libcpp::header_exportable_declarations::FileType::Header, "Header"}, |
| 26 |
{libcpp::header_exportable_declarations::FileType::ModulePartition, "ModulePartition"}, |
| 27 |
{libcpp::header_exportable_declarations::FileType::Module, "Module"}, |
| 28 |
{libcpp::header_exportable_declarations::FileType::CHeader, "CHeader"}, |
| 29 |
{libcpp::header_exportable_declarations::FileType::CompatModulePartition, "CompatModulePartition"}, |
| 30 |
{libcpp::header_exportable_declarations::FileType::CompatModule, "CompatModule"}}; |
| 31 |
return ArrayRef(Mapping); |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
namespace libcpp { |
| 36 |
header_exportable_declarations::header_exportable_declarations( |
| 37 |
llvm::StringRef name, clang::tidy::ClangTidyContext* context) |
| 38 |
: clang::tidy::ClangTidyCheck(name, context), |
| 39 |
filename_(Options.get("Filename", "")), |
| 40 |
file_type_(Options.get("FileType", header_exportable_declarations::FileType::Unknown)), |
| 41 |
extra_header_(Options.get("ExtraHeader", "")) { |
| 42 |
switch (file_type_) { |
| 43 |
case header_exportable_declarations::FileType::CHeader: |
| 44 |
case header_exportable_declarations::FileType::Header: |
| 45 |
if (filename_.empty()) |
| 46 |
llvm::errs() << "No filename is provided.\n"; |
| 47 |
if (extra_header_.empty()) |
| 48 |
extra_header_ = "$^"; // Use a never matching regex to silence an error message. |
| 49 |
break; |
| 50 |
case header_exportable_declarations::FileType::ModulePartition: |
| 51 |
case header_exportable_declarations::FileType::CompatModulePartition: |
| 52 |
if (filename_.empty()) |
| 53 |
llvm::errs() << "No filename is provided.\n"; |
| 54 |
[[fallthrough]]; |
| 55 |
case header_exportable_declarations::FileType::Module: |
| 56 |
case header_exportable_declarations::FileType::CompatModule: |
| 57 |
if (!extra_header_.empty()) |
| 58 |
llvm::errs() << "Extra headers are not allowed for modules.\n"; |
| 59 |
if (Options.get("SkipDeclarations")) |
| 60 |
llvm::errs() << "Modules may not skip declarations.\n"; |
| 61 |
if (Options.get("ExtraDeclarations")) |
| 62 |
llvm::errs() << "Modules may not have extra declarations.\n"; |
| 63 |
break; |
| 64 |
case header_exportable_declarations::FileType::Unknown: |
| 65 |
llvm::errs() << "No file type is provided.\n"; |
| 66 |
break; |
| 67 |
} |
| 68 |
|
| 69 |
std::optional<llvm::StringRef> list = Options.get("SkipDeclarations"); |
| 70 |
if (list) |
| 71 |
for (auto decl : std::views::split(*list, ' ')) { |
| 72 |
std::string s; |
| 73 |
std::ranges::copy(decl, std::back_inserter(s)); // use range based constructor |
| 74 |
skip_decls_.emplace(std::move(s)); |
| 75 |
} |
| 76 |
decls_ = skip_decls_; |
| 77 |
|
| 78 |
list = Options.get("ExtraDeclarations"); |
| 79 |
if (list) |
| 80 |
for (auto decl : std::views::split(*list, ' ')) |
| 81 |
std::cout << "using ::" << std::string_view{decl.data(), decl.size()} << ";\n"; |
| 82 |
} |
| 83 |
|
| 84 |
header_exportable_declarations::~header_exportable_declarations() { |
| 85 |
for (const auto& name : global_decls_) |
| 86 |
if (!skip_decls_.contains("std::" + name) && decls_.contains("std::" + name)) |
| 87 |
std::cout << "using ::" << name << ";\n"; |
| 88 |
} |
| 89 |
|
| 90 |
void header_exportable_declarations::registerMatchers(clang::ast_matchers::MatchFinder* finder) { |
| 91 |
// there are no public names in the Standard starting with an underscore, so |
| 92 |
// no need to check the strict rules. |
| 93 |
using namespace clang::ast_matchers; |
| 94 |
|
| 95 |
switch (file_type_) { |
| 96 |
case FileType::Header: |
| 97 |
finder->addMatcher( |
| 98 |
namedDecl( |
| 99 |
// Looks at the common locations where headers store their data |
| 100 |
// * header |
| 101 |
// * __header/*.h |
| 102 |
// * __fwd/header.h |
| 103 |
anyOf(isExpansionInFileMatching(("v1/__" + filename_ + "/").str()), |
| 104 |
isExpansionInFileMatching(extra_header_), |
| 105 |
isExpansionInFileMatching(("v1/__fwd/" + filename_ + "\\.h$").str()), |
| 106 |
isExpansionInFileMatching(("v1/" + filename_ + "$").str())), |
| 107 |
unless(hasAncestor(friendDecl()))) |
| 108 |
.bind("header_exportable_declarations"), |
| 109 |
this); |
| 110 |
break; |
| 111 |
case FileType::CHeader: |
| 112 |
// For C headers of the std.compat two matchers are used |
| 113 |
// - The cheader matcher; in libc++ these are never split in multiple |
| 114 |
// headers so limiting the declarations to that header works. |
| 115 |
// - The header.h; where the declarations of this header are provided |
| 116 |
// is not specified and depends on the libc used. Therefore it is not |
| 117 |
// possible to restrict the location in a portable way. |
| 118 |
finder->addMatcher(namedDecl().bind("cheader_exportable_declarations"), this); |
| 119 |
|
| 120 |
[[fallthrough]]; |
| 121 |
case FileType::ModulePartition: |
| 122 |
case FileType::CompatModulePartition: |
| 123 |
finder->addMatcher(namedDecl(isExpansionInFileMatching(filename_)).bind("header_exportable_declarations"), this); |
| 124 |
break; |
| 125 |
case FileType::Module: |
| 126 |
case FileType::CompatModule: |
| 127 |
finder->addMatcher(namedDecl().bind("header_exportable_declarations"), this); |
| 128 |
break; |
| 129 |
case header_exportable_declarations::FileType::Unknown: |
| 130 |
llvm::errs() << "This should be unreachable.\n"; |
| 131 |
break; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/// Returns the qualified name of a public declaration. |
| 136 |
/// |
| 137 |
/// There is a small issue with qualified names. Typically the name returned is |
| 138 |
/// in the namespace \c std instead of the namespace \c std::__1. Except when a |
| 139 |
/// name is declared both in the namespace \c std and in the namespace |
| 140 |
/// \c std::__1. In that case the returned value will adjust the name to use |
| 141 |
/// the namespace \c std. |
| 142 |
/// |
| 143 |
/// The reason this happens is due to some parts of libc++ using |
| 144 |
/// \code namespace std \endcode instead of |
| 145 |
/// \code _LIBCPP_BEGIN_NAMESPACE_STD \endcode |
| 146 |
/// Some examples |
| 147 |
/// * cstddef has bitwise operators for the type \c byte |
| 148 |
/// * exception has equality operators for the type \c exception_ptr |
| 149 |
/// * initializer_list has the functions \c begin and \c end |
| 150 |
/// |
| 151 |
/// When the named declaration uses a reserved name the result is an |
| 152 |
/// empty string. |
| 153 |
static std::string get_qualified_name(const clang::NamedDecl& decl) { |
| 154 |
std::string result = decl.getNameAsString(); |
| 155 |
// Reject reserved names (ignoring _ in global namespace). |
| 156 |
if (result.size() >= 2 && result[0] == '_') |
| 157 |
if (result[1] == '_' || std::isupper(result[1])) |
| 158 |
if (result != "_Exit") |
| 159 |
return ""; |
| 160 |
|
| 161 |
for (auto* context = llvm::dyn_cast_or_null<clang::NamespaceDecl>(decl.getDeclContext()); // |
| 162 |
context; |
| 163 |
context = llvm::dyn_cast_or_null<clang::NamespaceDecl>(context->getDeclContext())) { |
| 164 |
std::string ns = std::string(context->getName()); |
| 165 |
|
| 166 |
if (ns.starts_with("__")) { |
| 167 |
// When the reserved name is an inline namespace the namespace is |
| 168 |
// not added to the qualified name instead of removed. Libc++ uses |
| 169 |
// several inline namespace with reserved names. For example, |
| 170 |
// __1 for every declaration, __cpo in range-based algorithms. |
| 171 |
// |
| 172 |
// Note other inline namespaces are expanded. This resolves |
| 173 |
// ambiguity when two named declarations have the same name but in |
| 174 |
// different inline namespaces. These typically are the literal |
| 175 |
// conversion operators like operator""s which can be a |
| 176 |
// std::string or std::chrono::seconds. |
| 177 |
if (!context->isInline()) |
| 178 |
return ""; |
| 179 |
} else |
| 180 |
result = ns + "::" + result; |
| 181 |
} |
| 182 |
return result; |
| 183 |
} |
| 184 |
|
| 185 |
static bool is_viable_declaration(const clang::NamedDecl* decl) { |
| 186 |
// Declarations that are a subobject of a friend Declaration are automatically exported with the record itself. |
| 187 |
if (decl->getFriendObjectKind() != clang::Decl::FOK_None) |
| 188 |
return false; |
| 189 |
|
| 190 |
// *** Function declarations *** |
| 191 |
|
| 192 |
if (clang::CXXMethodDecl::classof(decl)) |
| 193 |
return false; |
| 194 |
|
| 195 |
if (clang::CXXDeductionGuideDecl::classof(decl)) |
| 196 |
return false; |
| 197 |
|
| 198 |
if (clang::FunctionDecl::classof(decl)) |
| 199 |
return true; |
| 200 |
|
| 201 |
if (clang::CXXConstructorDecl::classof(decl)) |
| 202 |
return false; |
| 203 |
|
| 204 |
// implicit constructors disallowed |
| 205 |
if (const auto* r = llvm::dyn_cast_or_null<clang::RecordDecl>(decl)) |
| 206 |
return !r->isLambda() && !r->isImplicit(); |
| 207 |
|
| 208 |
// *** Unconditionally accepted declarations *** |
| 209 |
return llvm::isa<clang::EnumDecl, clang::VarDecl, clang::ConceptDecl, clang::TypedefNameDecl, clang::UsingDecl>(decl); |
| 210 |
} |
| 211 |
|
| 212 |
/// Some declarations in the global namespace are exported from the std module. |
| 213 |
static bool is_global_name_exported_by_std_module(std::string_view name) { |
| 214 |
static const std::set<std::string_view> valid{ |
| 215 |
"operator delete", "operator delete[]", "operator new", "operator new[]"}; |
| 216 |
return valid.contains(name); |
| 217 |
} |
| 218 |
|
| 219 |
static bool is_valid_declaration_context( |
| 220 |
const clang::NamedDecl& decl, std::string_view name, header_exportable_declarations::FileType file_type) { |
| 221 |
const clang::DeclContext& context = *decl.getDeclContext(); |
| 222 |
if (context.isNamespace()) |
| 223 |
return true; |
| 224 |
|
| 225 |
if (context.isFunctionOrMethod() || context.isRecord()) |
| 226 |
return false; |
| 227 |
|
| 228 |
if (is_global_name_exported_by_std_module(name)) |
| 229 |
return true; |
| 230 |
|
| 231 |
return file_type != header_exportable_declarations::FileType::Header; |
| 232 |
} |
| 233 |
|
| 234 |
static bool is_module(header_exportable_declarations::FileType file_type) { |
| 235 |
switch (file_type) { |
| 236 |
case header_exportable_declarations::FileType::Module: |
| 237 |
case header_exportable_declarations::FileType::ModulePartition: |
| 238 |
case header_exportable_declarations::FileType::CompatModule: |
| 239 |
case header_exportable_declarations::FileType::CompatModulePartition: |
| 240 |
return true; |
| 241 |
|
| 242 |
case header_exportable_declarations::FileType::Header: |
| 243 |
case header_exportable_declarations::FileType::CHeader: |
| 244 |
return false; |
| 245 |
|
| 246 |
case header_exportable_declarations::FileType::Unknown: |
| 247 |
llvm::errs() << "This should be unreachable.\n"; |
| 248 |
return false; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
void header_exportable_declarations::check(const clang::ast_matchers::MatchFinder::MatchResult& result) { |
| 253 |
if (const auto* decl = result.Nodes.getNodeAs<clang::NamedDecl>("header_exportable_declarations"); decl != nullptr) { |
| 254 |
if (!is_viable_declaration(decl)) |
| 255 |
return; |
| 256 |
|
| 257 |
std::string name = get_qualified_name(*decl); |
| 258 |
if (name.empty()) |
| 259 |
return; |
| 260 |
|
| 261 |
// For modules only take the declarations exported. |
| 262 |
if (is_module(file_type_)) |
| 263 |
if (decl->getModuleOwnershipKind() != clang::Decl::ModuleOwnershipKind::VisibleWhenImported) |
| 264 |
return; |
| 265 |
|
| 266 |
if (!is_valid_declaration_context(*decl, name, file_type_)) |
| 267 |
return; |
| 268 |
|
| 269 |
if (decls_.contains(name)) { |
| 270 |
// For modules avoid exporting the same named declaration twice. For |
| 271 |
// header files this is common and valid. |
| 272 |
if (file_type_ == FileType::ModulePartition || file_type_ == FileType::CompatModulePartition) |
| 273 |
// After the warning the script continues. |
| 274 |
// The test will fail since modules have duplicated entries and headers not. |
| 275 |
llvm::errs() << "Duplicated export of '" << name << "'.\n"; |
| 276 |
else |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
// For named declarations in std this is valid |
| 281 |
// using std::foo; |
| 282 |
// for named declarations it is invalid to use |
| 283 |
// using bar; |
| 284 |
// Since fully qualifying named declarations in the std namespace is valid |
| 285 |
// using fully qualified names unconditionally. |
| 286 |
std::cout << "using ::" << std::string{name} << ";\n"; |
| 287 |
decls_.insert(name); |
| 288 |
} else if (const auto* decl = result.Nodes.getNodeAs<clang::NamedDecl>("cheader_exportable_declarations"); |
| 289 |
decl != nullptr) { |
| 290 |
if (decl->getDeclContext()->isNamespace()) |
| 291 |
return; |
| 292 |
|
| 293 |
if (!is_viable_declaration(decl)) |
| 294 |
return; |
| 295 |
|
| 296 |
std::string name = get_qualified_name(*decl); |
| 297 |
if (name.empty()) |
| 298 |
return; |
| 299 |
|
| 300 |
if (global_decls_.contains(name)) |
| 301 |
return; |
| 302 |
|
| 303 |
global_decls_.insert(name); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
} // namespace libcpp |