clang 18.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
25namespace clang {
26class Decl;
27class NamespaceDecl;
28class DeclContext;
29namespace tooling {
30namespace stdlib {
31
32class Symbol;
33enum class Lang { C = 0, CXX, LastValue = CXX };
34
35// A standard library header, such as <iostream>
36// Lightweight class, in fact just an index into a table.
37// C++ and C Library compatibility headers are considered different: e.g.
38// "<cstdio>" and "<stdio.h>" (and their symbols) are treated differently.
39class Header {
40public:
41 static std::vector<Header> all(Lang L = Lang::CXX);
42 // Name should contain the angle brackets, e.g. "<vector>".
43 static std::optional<Header> named(llvm::StringRef Name,
45
46 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Header &H) {
47 return OS << H.name();
48 }
49 llvm::StringRef name() const;
50
51private:
52 Header(unsigned ID, Lang Language) : ID(ID), Language(Language) {}
53 unsigned ID;
55
56 friend Symbol;
57 friend llvm::DenseMapInfo<Header>;
58 friend bool operator==(const Header &L, const Header &R) {
59 return L.ID == R.ID;
60 }
61};
62
63// A top-level standard library symbol, such as std::vector
64// Lightweight class, in fact just an index into a table.
65// C++ and C Standard Library symbols are considered distinct: e.g. std::printf
66// and ::printf are not treated as the same symbol.
67// The symbols do not contain macros right now, we don't have a reliable index
68// for them.
69class Symbol {
70public:
71 static std::vector<Symbol> all(Lang L = Lang::CXX);
72 /// \p Scope should have the trailing "::", for example:
73 /// named("std::chrono::", "system_clock")
74 static std::optional<Symbol>
75 named(llvm::StringRef Scope, llvm::StringRef Name, Lang Language = Lang::CXX);
76
77 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Symbol &S) {
78 return OS << S.qualifiedName();
79 }
80 llvm::StringRef scope() const;
81 llvm::StringRef name() const;
82 llvm::StringRef qualifiedName() const;
83 // The preferred header for this symbol (e.g. the suggested insertion).
84 std::optional<Header> header() const;
85 // Some symbols may be provided by multiple headers.
87
88private:
89 Symbol(unsigned ID, Lang Language) : ID(ID), Language(Language) {}
90 unsigned ID;
92
93 friend class Recognizer;
94 friend llvm::DenseMapInfo<Symbol>;
95 friend bool operator==(const Symbol &L, const Symbol &R) {
96 return L.ID == R.ID;
97 }
98};
99
100// A functor to find the stdlib::Symbol associated with a decl.
101//
102// For non-top-level decls (std::vector<int>::iterator), returns the top-level
103// symbol (std::vector).
105public:
106 Recognizer();
107 std::optional<Symbol> operator()(const Decl *D);
108
109private:
110 using NSSymbolMap = llvm::DenseMap<llvm::StringRef, unsigned>;
111 NSSymbolMap *namespaceSymbols(const DeclContext *DC, Lang L);
112 llvm::DenseMap<const DeclContext *, NSSymbolMap *> NamespaceCache;
113};
114
115} // namespace stdlib
116} // namespace tooling
117} // namespace clang
118
119namespace llvm {
120
121template <> struct DenseMapInfo<clang::tooling::stdlib::Header> {
125 }
129 }
131 return hash_value(H.ID);
132 }
135 return LHS == RHS;
136 }
137};
138
139template <> struct DenseMapInfo<clang::tooling::stdlib::Symbol> {
143 }
147 }
149 return hash_value(S.ID);
150 }
153 return LHS == RHS;
154 }
155};
156} // namespace llvm
157
158#endif // LLVM_CLANG_TOOLING_INCLUSIONS_STANDARDLIBRARY_H
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:85
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 ...
Language
The language for the input, used to select and validate the language standard and possible actions.
Definition: LangStandard.h:23
YAML serialization mapping.
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()