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