xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h (revision 5a38ef86d0b61900239c7913d24a05e7b88a58f0)
1 //===-- CPlusPlusLanguage.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 #ifndef LLDB_SOURCE_PLUGINS_LANGUAGE_CPLUSPLUS_CPLUSPLUSLANGUAGE_H
10 #define LLDB_SOURCE_PLUGINS_LANGUAGE_CPLUSPLUS_CPLUSPLUSLANGUAGE_H
11 
12 #include <set>
13 #include <vector>
14 
15 #include "llvm/ADT/StringRef.h"
16 
17 #include "Plugins/Language/ClangCommon/ClangHighlighter.h"
18 #include "lldb/Target/Language.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/lldb-private.h"
21 
22 namespace lldb_private {
23 
24 class CPlusPlusLanguage : public Language {
25   ClangHighlighter m_highlighter;
26 
27 public:
28   class MethodName {
29   public:
30     MethodName()
31         : m_full(), m_basename(), m_context(), m_arguments(), m_qualifiers() {}
32 
33     MethodName(ConstString s)
34         : m_full(s), m_basename(), m_context(), m_arguments(), m_qualifiers(),
35           m_parsed(false), m_parse_error(false) {}
36 
37     void Clear();
38 
39     bool IsValid() {
40       if (!m_parsed)
41         Parse();
42       if (m_parse_error)
43         return false;
44       return (bool)m_full;
45     }
46 
47     ConstString GetFullName() const { return m_full; }
48 
49     std::string GetScopeQualifiedName();
50 
51     llvm::StringRef GetBasename();
52 
53     llvm::StringRef GetContext();
54 
55     llvm::StringRef GetArguments();
56 
57     llvm::StringRef GetQualifiers();
58 
59   protected:
60     void Parse();
61     bool TrySimplifiedParse();
62 
63     ConstString m_full; // Full name:
64                         // "lldb::SBTarget::GetBreakpointAtIndex(unsigned int)
65                         // const"
66     llvm::StringRef m_basename;   // Basename:     "GetBreakpointAtIndex"
67     llvm::StringRef m_context;    // Decl context: "lldb::SBTarget"
68     llvm::StringRef m_arguments;  // Arguments:    "(unsigned int)"
69     llvm::StringRef m_qualifiers; // Qualifiers:   "const"
70     bool m_parsed = false;
71     bool m_parse_error = false;
72   };
73 
74   CPlusPlusLanguage() = default;
75 
76   ~CPlusPlusLanguage() override = default;
77 
78   lldb::LanguageType GetLanguageType() const override {
79     return lldb::eLanguageTypeC_plus_plus;
80   }
81 
82   std::unique_ptr<TypeScavenger> GetTypeScavenger() override;
83   lldb::TypeCategoryImplSP GetFormatters() override;
84 
85   HardcodedFormatters::HardcodedSummaryFinder GetHardcodedSummaries() override;
86 
87   HardcodedFormatters::HardcodedSyntheticFinder
88   GetHardcodedSynthetics() override;
89 
90   bool IsNilReference(ValueObject &valobj) override;
91 
92   llvm::StringRef GetNilReferenceSummaryString() override { return "nullptr"; }
93 
94   bool IsSourceFile(llvm::StringRef file_path) const override;
95 
96   const Highlighter *GetHighlighter() const override { return &m_highlighter; }
97 
98   // Static Functions
99   static void Initialize();
100 
101   static void Terminate();
102 
103   static lldb_private::Language *CreateInstance(lldb::LanguageType language);
104 
105   static lldb_private::ConstString GetPluginNameStatic();
106 
107   bool SymbolNameFitsToLanguage(Mangled mangled) const override;
108 
109   static bool IsCPPMangledName(llvm::StringRef name);
110 
111   // Extract C++ context and identifier from a string using heuristic matching
112   // (as opposed to
113   // CPlusPlusLanguage::MethodName which has to have a fully qualified C++ name
114   // with parens and arguments.
115   // If the name is a lone C identifier (e.g. C) or a qualified C identifier
116   // (e.g. A::B::C) it will return true,
117   // and identifier will be the identifier (C and C respectively) and the
118   // context will be "" and "A::B" respectively.
119   // If the name fails the heuristic matching for a qualified or unqualified
120   // C/C++ identifier, then it will return false
121   // and identifier and context will be unchanged.
122 
123   static bool ExtractContextAndIdentifier(const char *name,
124                                           llvm::StringRef &context,
125                                           llvm::StringRef &identifier);
126 
127   // Given a mangled function name, calculates some alternative manglings since
128   // the compiler mangling may not line up with the symbol we are expecting
129   static uint32_t
130   FindAlternateFunctionManglings(const ConstString mangled,
131                                  std::set<ConstString> &candidates);
132 
133   // PluginInterface protocol
134   ConstString GetPluginName() override;
135 
136   uint32_t GetPluginVersion() override;
137 };
138 
139 } // namespace lldb_private
140 
141 #endif // LLDB_SOURCE_PLUGINS_LANGUAGE_CPLUSPLUS_CPLUSPLUSLANGUAGE_H
142