clang 20.0.0git
StandardLibrary.h
Go to the documentation of this file.
1//===--- StandardLibrary.h --------------------------------------*- C++ -*-===//
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/// \file
10/// Provides an interface for querying information about C and C++ Standard
11/// Library headers and symbols.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_TOOLING_INCLUSIONS_STANDARDLIBRARY_H
16#define LLVM_CLANG_TOOLING_INCLUSIONS_STANDARDLIBRARY_H
17
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/Hashing.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/raw_ostream.h"
22#include <optional>
23#include <string>
24#include <vector>
25
26namespace clang {
27class Decl;
28class NamespaceDecl;
29class DeclContext;
30namespace tooling {
31namespace stdlib {
32
33class Symbol;
34enum class Lang { C = 0, CXX, LastValue = CXX };
35
36// A standard library header, such as <iostream>
37// Lightweight class, in fact just an index into a table.
38// C++ and C Library compatibility headers are considered different: e.g.
39// "<cstdio>" and "<stdio.h>" (and their symbols) are treated differently.
40class Header {
41public:
42 static std::vector<Header> all(Lang L = Lang::CXX);
43 // Name should contain the angle brackets, e.g. "<vector>".
44 static std::optional<Header> named(llvm::StringRef Name,
46
47 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Header &H) {
48 return OS << H.name();
49 }
50 llvm::StringRef name() const;
51
52private:
53 Header(unsigned ID, Lang Language) : ID(ID), Language(Language) {}
54 unsigned ID;
56
57 friend Symbol;
58 friend llvm::DenseMapInfo<Header>;
59 friend bool operator==(const Header &L, const Header &R) {
60 return L.ID == R.ID;
61 }
62};
63
64// A top-level standard library symbol, such as std::vector
65// Lightweight class, in fact just an index into a table.
66// C++ and C Standard Library symbols are considered distinct: e.g. std::printf
67// and ::printf are not treated as the same symbol.
68// The symbols do not contain macros right now, we don't have a reliable index
69// for them.
70class Symbol {
71public:
72 static std::vector<Symbol> all(Lang L = Lang::CXX);
73 /// \p Scope should have the trailing "::", for example:
74 /// named("std::chrono::", "system_clock")
75 static std::optional<Symbol>
76 named(llvm::StringRef Scope, llvm::StringRef Name, Lang Language = Lang::CXX);
77
78 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Symbol &S) {
79 return OS << S.qualifiedName();
80 }
81 llvm::StringRef scope() const;
82 llvm::StringRef name() const;
83 llvm::StringRef qualifiedName() const;
84 // The preferred header for this symbol (e.g. the suggested insertion).
85 std::optional<Header> header() const;
86 // Some symbols may be provided by multiple headers.
88
89private:
90 Symbol(unsigned ID, Lang Language) : ID(ID), Language(Language) {}
91 unsigned ID;
93
94 friend class Recognizer;
95 friend llvm::DenseMapInfo<Symbol>;
96 friend bool operator==(const Symbol &L, const Symbol &R) {
97 return L.ID == R.ID;
98 }
99};
100
101// A functor to find the stdlib::Symbol associated with a decl.
102//
103// For non-top-level decls (std::vector<int>::iterator), returns the top-level
104// symbol (std::vector).
106public:
107 Recognizer();
108 std::optional<Symbol> operator()(const Decl *D);
109
110private:
111 using NSSymbolMap = llvm::DenseMap<llvm::StringRef, unsigned>;
112 NSSymbolMap *namespaceSymbols(const DeclContext *DC, Lang L);
113 llvm::DenseMap<const DeclContext *, NSSymbolMap *> NamespaceCache;
114};
115
116} // namespace stdlib
117} // namespace tooling
118} // namespace clang
119
120namespace llvm {
121
122template <> struct DenseMapInfo<clang::tooling::stdlib::Header> {
126 }
130 }
132 return hash_value(H.ID);
133 }
136 return LHS == RHS;
137 }
138};
139
140template <> struct DenseMapInfo<clang::tooling::stdlib::Symbol> {
144 }
148 }
150 return hash_value(S.ID);
151 }
154 return LHS == RHS;
155 }
156};
157} // namespace llvm
158
159#endif // LLVM_CLANG_TOOLING_INCLUSIONS_STANDARDLIBRARY_H
const Decl * D
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1435
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
static std::optional< Header > named(llvm::StringRef Name, Lang Language=Lang::CXX)
friend llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Header &H)
friend bool operator==(const Header &L, const Header &R)
static std::vector< Header > all(Lang L=Lang::CXX)
llvm::StringRef name() const
std::optional< Symbol > operator()(const Decl *D)
static std::optional< Symbol > named(llvm::StringRef Scope, llvm::StringRef Name, Lang Language=Lang::CXX)
Scope should have the trailing "::", for example: named("std::chrono::", "system_clock")
llvm::StringRef name() const
llvm::StringRef scope() const
llvm::SmallVector< Header > headers() const
llvm::StringRef qualifiedName() const
friend llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Symbol &S)
static std::vector< Symbol > all(Lang L=Lang::CXX)
std::optional< Header > header() const
friend bool operator==(const Symbol &L, const Symbol &R)
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
The JSON file list parser is used to communicate input to InstallAPI.
Language
The language for the input, used to select and validate the language standard and possible actions.
Definition: LangStandard.h:23
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
hash_code hash_value(const clang::tooling::dependencies::ModuleID &ID)
static unsigned getHashValue(const clang::tooling::stdlib::Header &H)
static clang::tooling::stdlib::Header getTombstoneKey()
static bool isEqual(const clang::tooling::stdlib::Header &LHS, const clang::tooling::stdlib::Header &RHS)
static clang::tooling::stdlib::Header getEmptyKey()
static unsigned getHashValue(const clang::tooling::stdlib::Symbol &S)
static clang::tooling::stdlib::Symbol getEmptyKey()
static bool isEqual(const clang::tooling::stdlib::Symbol &LHS, const clang::tooling::stdlib::Symbol &RHS)
static clang::tooling::stdlib::Symbol getTombstoneKey()