File: header_exportable_declarations.cpp

package info (click to toggle)
llvm-toolchain-18 1%3A18.1.8-20
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,909,296 kB
  • sloc: cpp: 6,667,975; ansic: 1,440,452; asm: 883,619; python: 230,549; objc: 76,880; f90: 74,238; lisp: 35,989; pascal: 16,571; sh: 10,229; perl: 7,459; ml: 5,047; awk: 3,523; makefile: 2,992; javascript: 2,149; xml: 892; fortran: 649; cs: 573
file content (341 lines) | stat: -rw-r--r-- 13,375 bytes parent folder | download | duplicates (2)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//===----------------------------------------------------------------------===//
//
// 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 "clang-tidy/ClangTidyCheck.h"
#include "clang-tidy/ClangTidyModuleRegistry.h"

#include "clang/Basic/Module.h"

#include "llvm/ADT/ArrayRef.h"

#include "header_exportable_declarations.hpp"

#include <iostream>
#include <iterator>
#include <ranges>
#include <algorithm>

template <>
struct clang::tidy::OptionEnumMapping<libcpp::header_exportable_declarations::FileType> {
  static llvm::ArrayRef<std::pair<libcpp::header_exportable_declarations::FileType, llvm::StringRef>> getEnumMapping() {
    static constexpr std::pair<libcpp::header_exportable_declarations::FileType, llvm::StringRef> Mapping[] = {
        {libcpp::header_exportable_declarations::FileType::Header, "Header"},
        {libcpp::header_exportable_declarations::FileType::ModulePartition, "ModulePartition"},
        {libcpp::header_exportable_declarations::FileType::Module, "Module"},
        {libcpp::header_exportable_declarations::FileType::CHeader, "CHeader"},
        {libcpp::header_exportable_declarations::FileType::CompatModulePartition, "CompatModulePartition"},
        {libcpp::header_exportable_declarations::FileType::CompatModule, "CompatModule"}};
    return ArrayRef(Mapping);
  }
};

namespace libcpp {
header_exportable_declarations::header_exportable_declarations(
    llvm::StringRef name, clang::tidy::ClangTidyContext* context)
    : clang::tidy::ClangTidyCheck(name, context),
      filename_(Options.get("Filename", "")),
      file_type_(Options.get("FileType", header_exportable_declarations::FileType::Unknown)),
      extra_header_(Options.get("ExtraHeader", "")) {
  switch (file_type_) {
  case header_exportable_declarations::FileType::CHeader:
  case header_exportable_declarations::FileType::Header:
    if (filename_.empty())
      llvm::errs() << "No filename is provided.\n";
    if (extra_header_.empty())
      extra_header_ = "$^"; // Use a never matching regex to silence an error message.
    break;
  case header_exportable_declarations::FileType::ModulePartition:
  case header_exportable_declarations::FileType::CompatModulePartition:
    if (filename_.empty())
      llvm::errs() << "No filename is provided.\n";
    [[fallthrough]];
  case header_exportable_declarations::FileType::Module:
  case header_exportable_declarations::FileType::CompatModule:
    if (!extra_header_.empty())
      llvm::errs() << "Extra headers are not allowed for modules.\n";
    if (Options.get("SkipDeclarations"))
      llvm::errs() << "Modules may not skip declarations.\n";
    if (Options.get("ExtraDeclarations"))
      llvm::errs() << "Modules may not have extra declarations.\n";
    break;
  case header_exportable_declarations::FileType::Unknown:
    llvm::errs() << "No file type is provided.\n";
    break;
  }

  std::optional<llvm::StringRef> list = Options.get("SkipDeclarations");
  // TODO(LLVM-17) Remove clang 15 work-around.
#if defined(__clang_major__) && __clang_major__ < 16
  if (list) {
    std::string_view s = *list;
    auto b             = s.begin();
    auto e             = std::find(b, s.end(), ' ');
    while (b != e) {
      skip_decls_.emplace(b, e);
      if (e == s.end())
        break;
      b = e + 1;
      e = std::find(b, s.end(), ' ');
    }
  }
#else  // defined(__clang_major__) && __clang_major__ < 16
  if (list)
    for (auto decl : std::views::split(*list, ' ')) {
      std::string s;
      std::ranges::copy(decl, std::back_inserter(s)); // use range based constructor
      skip_decls_.emplace(std::move(s));
    }
#endif // defined(__clang_major__) && __clang_major__ < 16
  decls_ = skip_decls_;

  list = Options.get("ExtraDeclarations");
  // TODO(LLVM-17) Remove clang 15 work-around.
#if defined(__clang_major__) && __clang_major__ < 16
  if (list) {
    std::string_view s = *list;
    auto b             = s.begin();
    auto e             = std::find(b, s.end(), ' ');
    while (b != e) {
      std::cout << "using ::" << std::string_view{b, e} << ";\n";
      if (e == s.end())
        break;
      b = e + 1;
      e = std::find(b, s.end(), ' ');
    }
  }
#else  // defined(__clang_major__) && __clang_major__ < 16
  if (list)
    for (auto decl : std::views::split(*list, ' '))
      std::cout << "using ::" << std::string_view{decl.data(), decl.size()} << ";\n";
#endif // defined(__clang_major__) && __clang_major__ < 16
}

header_exportable_declarations::~header_exportable_declarations() {
  for (const auto& name : global_decls_)
    if (!skip_decls_.contains("std::" + name) && decls_.contains("std::" + name))
      std::cout << "using ::" << name << ";\n";
}

void header_exportable_declarations::registerMatchers(clang::ast_matchers::MatchFinder* finder) {
  // there are no public names in the Standard starting with an underscore, so
  // no need to check the strict rules.
  using namespace clang::ast_matchers;

  switch (file_type_) {
  case FileType::Header:
    finder->addMatcher(
        namedDecl(
            // Looks at the common locations where headers store their data
            // * header
            // * __header/*.h
            // * __fwd/header.h
            anyOf(isExpansionInFileMatching(("v1/__" + filename_ + "/").str()),
                  isExpansionInFileMatching(extra_header_),
                  isExpansionInFileMatching(("v1/__fwd/" + filename_ + "\\.h$").str()),
                  isExpansionInFileMatching(("v1/" + filename_ + "$").str())),
            unless(hasAncestor(friendDecl())))
            .bind("header_exportable_declarations"),
        this);
    break;
  case FileType::CHeader:
    // For C headers of the std.compat two matchers are used
    // - The cheader matcher; in libc++ these are never split in multiple
    //   headers so limiting the declarations to that header works.
    // - The header.h; where the declarations of this header are provided
    //   is not specified and depends on the libc used. Therefore it is not
    //   possible to restrict the location in a portable way.
    finder->addMatcher(namedDecl().bind("cheader_exportable_declarations"), this);

    [[fallthrough]];
  case FileType::ModulePartition:
  case FileType::CompatModulePartition:
    finder->addMatcher(namedDecl(isExpansionInFileMatching(filename_)).bind("header_exportable_declarations"), this);
    break;
  case FileType::Module:
  case FileType::CompatModule:
    finder->addMatcher(namedDecl().bind("header_exportable_declarations"), this);
    break;
  case header_exportable_declarations::FileType::Unknown:
    llvm::errs() << "This should be unreachable.\n";
    break;
  }
}

/// Returns the qualified name of a public declaration.
///
/// There is a small issue with qualified names. Typically the name returned is
/// in the namespace \c std instead of the namespace \c std::__1. Except when a
/// name is declared both in the namespace \c std and in the namespace
/// \c std::__1. In that case the returned value will adjust the name to use
/// the namespace \c std.
///
/// The reason this happens is due to some parts of libc++ using
/// \code namespace std \endcode instead of
/// \code _LIBCPP_BEGIN_NAMESPACE_STD \endcode
/// Some examples
/// * cstddef has bitwise operators for the type \c byte
/// * exception has equality operators for the type \c exception_ptr
/// * initializer_list has the functions \c begin and \c end
///
/// When the named declaration uses a reserved name the result is an
/// empty string.
static std::string get_qualified_name(const clang::NamedDecl& decl) {
  std::string result = decl.getNameAsString();
  // Reject reserved names (ignoring _ in global namespace).
  if (result.size() >= 2 && result[0] == '_')
    if (result[1] == '_' || std::isupper(result[1]))
      if (result != "_Exit")
        return "";

  for (auto* context = llvm::dyn_cast_or_null<clang::NamespaceDecl>(decl.getDeclContext()); //
       context;
       context = llvm::dyn_cast_or_null<clang::NamespaceDecl>(context->getDeclContext())) {
    std::string ns = std::string(context->getName());

    if (ns.starts_with("__")) {
      // When the reserved name is an inline namespace the namespace is
      // not added to the qualified name instead of removed. Libc++ uses
      // several inline namespace with reserved names. For example,
      // __1 for every declaration, __cpo in range-based algorithms.
      //
      // Note other inline namespaces are expanded. This resolves
      // ambiguity when two named declarations have the same name but in
      // different inline namespaces. These typically are the literal
      // conversion operators like operator""s which can be a
      // std::string or std::chrono::seconds.
      if (!context->isInline())
        return "";
    } else
      result = ns + "::" + result;
  }
  return result;
}

static bool is_viable_declaration(const clang::NamedDecl* decl) {
  // Declarations that are a subobject of a friend Declaration are automatically exported with the record itself.
  if (decl->getFriendObjectKind() != clang::Decl::FOK_None)
    return false;

  // *** Function declarations ***

  if (clang::CXXMethodDecl::classof(decl))
    return false;

  if (clang::CXXDeductionGuideDecl::classof(decl))
    return false;

  if (clang::FunctionDecl::classof(decl))
    return true;

  if (clang::CXXConstructorDecl::classof(decl))
    return false;

  // implicit constructors disallowed
  if (const auto* r = llvm::dyn_cast_or_null<clang::RecordDecl>(decl))
    return !r->isLambda() && !r->isImplicit();

  // *** Unconditionally accepted declarations ***
  return llvm::isa<clang::EnumDecl, clang::VarDecl, clang::ConceptDecl, clang::TypedefNameDecl, clang::UsingDecl>(decl);
}

/// Some declarations in the global namespace are exported from the std module.
static bool is_global_name_exported_by_std_module(std::string_view name) {
  static const std::set<std::string_view> valid{
      "operator delete", "operator delete[]", "operator new", "operator new[]"};
  return valid.contains(name);
}

static bool is_valid_declaration_context(
    const clang::NamedDecl& decl, std::string_view name, header_exportable_declarations::FileType file_type) {
  const clang::DeclContext& context = *decl.getDeclContext();
  if (context.isNamespace())
    return true;

  if (context.isFunctionOrMethod() || context.isRecord())
    return false;

  if (is_global_name_exported_by_std_module(name))
    return true;

  return file_type != header_exportable_declarations::FileType::Header;
}

static bool is_module(header_exportable_declarations::FileType file_type) {
  switch (file_type) {
  case header_exportable_declarations::FileType::Module:
  case header_exportable_declarations::FileType::ModulePartition:
  case header_exportable_declarations::FileType::CompatModule:
  case header_exportable_declarations::FileType::CompatModulePartition:
    return true;

  case header_exportable_declarations::FileType::Header:
  case header_exportable_declarations::FileType::CHeader:
    return false;

  case header_exportable_declarations::FileType::Unknown:
    llvm::errs() << "This should be unreachable.\n";
    return false;
  }
}

void header_exportable_declarations::check(const clang::ast_matchers::MatchFinder::MatchResult& result) {
  if (const auto* decl = result.Nodes.getNodeAs<clang::NamedDecl>("header_exportable_declarations"); decl != nullptr) {
    if (!is_viable_declaration(decl))
      return;

    std::string name = get_qualified_name(*decl);
    if (name.empty())
      return;

    // For modules only take the declarations exported.
    if (is_module(file_type_))
      if (decl->getModuleOwnershipKind() != clang::Decl::ModuleOwnershipKind::VisibleWhenImported)
        return;

    if (!is_valid_declaration_context(*decl, name, file_type_))
      return;

    if (decls_.contains(name)) {
      // For modules avoid exporting the same named declaration twice. For
      // header files this is common and valid.
      if (file_type_ == FileType::ModulePartition || file_type_ == FileType::CompatModulePartition)
        // After the warning the script continues.
        // The test will fail since modules have duplicated entries and headers not.
        llvm::errs() << "Duplicated export of '" << name << "'.\n";
      else
        return;
    }

    // For named declarations in std this is valid
    //   using std::foo;
    // for named declarations it is invalid to use
    //   using bar;
    // Since fully qualifying named declarations in the std namespace is valid
    // using fully qualified names unconditionally.
    std::cout << "using ::" << std::string{name} << ";\n";
    decls_.insert(name);
  } else if (const auto* decl = result.Nodes.getNodeAs<clang::NamedDecl>("cheader_exportable_declarations");
             decl != nullptr) {
    if (decl->getDeclContext()->isNamespace())
      return;

    if (!is_viable_declaration(decl))
      return;

    std::string name = get_qualified_name(*decl);
    if (name.empty())
      return;

    if (global_decls_.contains(name))
      return;

    global_decls_.insert(name);
  }
}

} // namespace libcpp