LLDB  mainline
CPlusPlusNameParser.h
Go to the documentation of this file.
1 //===-- CPlusPlusNameParser.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_CPLUSPLUSNAMEPARSER_H
10 #define LLDB_SOURCE_PLUGINS_LANGUAGE_CPLUSPLUS_CPLUSPLUSNAMEPARSER_H
11 
12 #include "clang/Lex/Lexer.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 
18 #include "lldb/lldb-private.h"
19 
20 namespace lldb_private {
21 
22 // Helps to validate and obtain various parts of C++ definitions.
24 public:
25  CPlusPlusNameParser(llvm::StringRef text) : m_text(text) { ExtractTokens(); }
26 
27  struct ParsedName {
28  llvm::StringRef basename;
29  llvm::StringRef context;
30  };
31 
32  struct ParsedFunction {
34  llvm::StringRef arguments;
35  llvm::StringRef qualifiers;
36  };
37 
38  // Treats given text as a function definition and parses it.
39  // Function definition might or might not have a return type and this should
40  // change parsing result.
41  // Examples:
42  // main(int, chat const*)
43  // T fun(int, bool)
44  // std::vector<int>::push_back(int)
45  // int& map<int, pair<short, int>>::operator[](short) const
46  // int (*get_function(const chat *))()
47  llvm::Optional<ParsedFunction> ParseAsFunctionDefinition();
48 
49  // Treats given text as a potentially nested name of C++ entity (function,
50  // class, field) and parses it.
51  // Examples:
52  // main
53  // fun
54  // std::vector<int>::push_back
55  // map<int, pair<short, int>>::operator[]
56  // func<C>(int, C&)::nested_class::method
57  llvm::Optional<ParsedName> ParseAsFullName();
58 
59 private:
60  // A C++ definition to parse.
61  llvm::StringRef m_text;
62  // Tokens extracted from m_text.
63  llvm::SmallVector<clang::Token, 30> m_tokens;
64  // Index of the next token to look at from m_tokens.
65  size_t m_next_token_index = 0;
66 
67  // Range of tokens saved in m_next_token_index.
68  struct Range {
69  size_t begin_index = 0;
70  size_t end_index = 0;
71 
72  Range() = default;
73  Range(size_t begin, size_t end) : begin_index(begin), end_index(end) {
74  assert(end >= begin);
75  }
76 
77  size_t size() const { return end_index - begin_index; }
78 
79  bool empty() const { return size() == 0; }
80  };
81 
85  };
86 
87  // Bookmark automatically restores parsing position (m_next_token_index)
88  // when destructed unless it's manually removed with Remove().
89  class Bookmark {
90  public:
91  Bookmark(size_t &position)
92  : m_position(position), m_position_value(position) {}
93  Bookmark(const Bookmark &) = delete;
96  m_restore(b.m_restore) {
97  b.Remove();
98  }
99  Bookmark &operator=(Bookmark &&) = delete;
100  Bookmark &operator=(const Bookmark &) = delete;
101 
102  void Remove() { m_restore = false; }
103  size_t GetSavedPosition() { return m_position_value; }
105  if (m_restore) {
107  }
108  }
109 
110  private:
111  size_t &m_position;
113  bool m_restore = true;
114  };
115 
116  bool HasMoreTokens();
117  void Advance();
118  void TakeBack();
119  bool ConsumeToken(clang::tok::TokenKind kind);
120  template <typename... Ts> bool ConsumeToken(Ts... kinds);
122  size_t GetCurrentPosition();
123  clang::Token &Peek();
124  bool ConsumeBrackets(clang::tok::TokenKind left, clang::tok::TokenKind right);
125 
126  llvm::Optional<ParsedFunction> ParseFunctionImpl(bool expect_return_type);
127 
128  // Parses functions returning function pointers 'string (*f(int x))(float y)'
129  llvm::Optional<ParsedFunction> ParseFuncPtr(bool expect_return_type);
130 
131  // Consumes function arguments enclosed within '(' ... ')'
132  bool ConsumeArguments();
133 
134  // Consumes template arguments enclosed within '<' ... '>'
135  bool ConsumeTemplateArgs();
136 
137  // Consumes '(anonymous namespace)'
139 
140  // Consumes '{lambda ...}'
141  bool ConsumeLambda();
142 
143  // Consumes operator declaration like 'operator *' or 'operator delete []'
144  bool ConsumeOperator();
145 
146  // Skips 'const' and 'volatile'
147  void SkipTypeQualifiers();
148 
149  // Skips 'const', 'volatile', '&', '&&' in the end of the function.
150  void SkipFunctionQualifiers();
151 
152  // Consumes built-in types like 'int' or 'unsigned long long int'
153  bool ConsumeBuiltinType();
154 
155  // Consumes types defined via decltype keyword.
156  bool ConsumeDecltype();
157 
158  // Skips 'const' and 'volatile'
159  void SkipPtrsAndRefs();
160 
161  // Consumes things like 'const * const &'
162  bool ConsumePtrsAndRefs();
163 
164  // Consumes full type name like 'Namespace::Class<int>::Method()::InnerClass'
165  bool ConsumeTypename();
166 
167  llvm::Optional<ParsedNameRanges> ParseFullNameImpl();
168  llvm::StringRef GetTextForRange(const Range &range);
169 
170  // Populate m_tokens by calling clang lexer on m_text.
171  void ExtractTokens();
172 };
173 
174 } // namespace lldb_private
175 
176 #endif // LLDB_SOURCE_PLUGINS_LANGUAGE_CPLUSPLUS_CPLUSPLUSNAMEPARSER_H
lldb_private::CPlusPlusNameParser::Peek
clang::Token & Peek()
Definition: CPlusPlusNameParser.cpp:99
lldb_private::CPlusPlusNameParser::SkipPtrsAndRefs
void SkipPtrsAndRefs()
Definition: CPlusPlusNameParser.cpp:454
lldb_private::CPlusPlusNameParser::ConsumeTemplateArgs
bool ConsumeTemplateArgs()
Definition: CPlusPlusNameParser.cpp:178
lldb_private::CPlusPlusNameParser::TakeBack
void TakeBack()
Definition: CPlusPlusNameParser.cpp:69
lldb_private::CPlusPlusNameParser::ParsedFunction
Definition: CPlusPlusNameParser.h:32
lldb_private::CPlusPlusNameParser::Range::begin_index
size_t begin_index
Definition: CPlusPlusNameParser.h:69
lldb_private::CPlusPlusNameParser::Range::empty
bool empty() const
Definition: CPlusPlusNameParser.h:79
lldb_private::CPlusPlusNameParser::ParsedFunction::name
ParsedName name
Definition: CPlusPlusNameParser.h:33
lldb_private::CPlusPlusNameParser::ConsumeToken
bool ConsumeToken(clang::tok::TokenKind kind)
lldb_private::CPlusPlusNameParser::ConsumeDecltype
bool ConsumeDecltype()
Definition: CPlusPlusNameParser.cpp:470
lldb_private::CPlusPlusNameParser::ParseAsFunctionDefinition
llvm::Optional< ParsedFunction > ParseAsFunctionDefinition()
Definition: CPlusPlusNameParser.cpp:23
lldb_private::CPlusPlusNameParser::m_tokens
llvm::SmallVector< clang::Token, 30 > m_tokens
Definition: CPlusPlusNameParser.h:63
lldb_private::CPlusPlusNameParser::ParsedName::context
llvm::StringRef context
Definition: CPlusPlusNameParser.h:29
lldb_private::CPlusPlusNameParser::ConsumeTypename
bool ConsumeTypename()
Definition: CPlusPlusNameParser.cpp:482
lldb_private::CPlusPlusNameParser::ParsedNameRanges::basename_range
Range basename_range
Definition: CPlusPlusNameParser.h:83
lldb_private::CPlusPlusNameParser::GetTextForRange
llvm::StringRef GetTextForRange(const Range &range)
Definition: CPlusPlusNameParser.cpp:634
lldb_private::CPlusPlusNameParser::Bookmark::m_position
size_t & m_position
Definition: CPlusPlusNameParser.h:111
lldb_private::CPlusPlusNameParser::Range::Range
Range(size_t begin, size_t end)
Definition: CPlusPlusNameParser.h:73
lldb_private::CPlusPlusNameParser::ParseFuncPtr
llvm::Optional< ParsedFunction > ParseFuncPtr(bool expect_return_type)
Definition: CPlusPlusNameParser.cpp:137
lldb_private::CPlusPlusNameParser
Definition: CPlusPlusNameParser.h:23
lldb_private::CPlusPlusNameParser::ParsedNameRanges
Definition: CPlusPlusNameParser.h:82
lldb_private::CPlusPlusNameParser::Bookmark::operator=
Bookmark & operator=(Bookmark &&)=delete
lldb_private::CPlusPlusNameParser::Range::end_index
size_t end_index
Definition: CPlusPlusNameParser.h:70
lldb_private::CPlusPlusNameParser::Bookmark::m_restore
bool m_restore
Definition: CPlusPlusNameParser.h:113
lldb_private::CPlusPlusNameParser::ConsumeLambda
bool ConsumeLambda()
Definition: CPlusPlusNameParser.cpp:275
lldb_private::CPlusPlusNameParser::ParsedFunction::qualifiers
llvm::StringRef qualifiers
Definition: CPlusPlusNameParser.h:35
lldb_private::CPlusPlusNameParser::ParseAsFullName
llvm::Optional< ParsedName > ParseAsFullName()
Definition: CPlusPlusNameParser.cpp:50
lldb_private::CPlusPlusNameParser::m_next_token_index
size_t m_next_token_index
Definition: CPlusPlusNameParser.h:65
lldb_private::CPlusPlusNameParser::Bookmark::GetSavedPosition
size_t GetSavedPosition()
Definition: CPlusPlusNameParser.h:103
lldb_private::CPlusPlusNameParser::ParsedFunction::arguments
llvm::StringRef arguments
Definition: CPlusPlusNameParser.h:34
lldb_private::CPlusPlusNameParser::Bookmark::~Bookmark
~Bookmark()
Definition: CPlusPlusNameParser.h:104
lldb_private::CPlusPlusNameParser::Bookmark::Bookmark
Bookmark(Bookmark &&b)
Definition: CPlusPlusNameParser.h:94
lldb_private::CPlusPlusNameParser::ConsumeAnonymousNamespace
bool ConsumeAnonymousNamespace()
Definition: CPlusPlusNameParser.cpp:251
lldb_private::CPlusPlusNameParser::HasMoreTokens
bool HasMoreTokens()
Definition: CPlusPlusNameParser.cpp:63
lldb_private::CPlusPlusNameParser::SkipFunctionQualifiers
void SkipFunctionQualifiers()
Definition: CPlusPlusNameParser.cpp:413
lldb_private::CPlusPlusNameParser::ConsumeArguments
bool ConsumeArguments()
Definition: CPlusPlusNameParser.cpp:174
lldb_private::CPlusPlusNameParser::ParsedNameRanges::context_range
Range context_range
Definition: CPlusPlusNameParser.h:84
lldb_private::CPlusPlusNameParser::ParsedName::basename
llvm::StringRef basename
Definition: CPlusPlusNameParser.h:28
lldb_private::CPlusPlusNameParser::Range
Definition: CPlusPlusNameParser.h:68
lldb-private.h
lldb_private::CPlusPlusNameParser::GetCurrentPosition
size_t GetCurrentPosition()
Definition: CPlusPlusNameParser.cpp:97
lldb_private::CPlusPlusNameParser::Advance
void Advance()
Definition: CPlusPlusNameParser.cpp:67
lldb_private::CPlusPlusNameParser::Bookmark::m_position_value
size_t m_position_value
Definition: CPlusPlusNameParser.h:112
lldb_private::CPlusPlusNameParser::ConsumeBrackets
bool ConsumeBrackets(clang::tok::TokenKind left, clang::tok::TokenKind right)
Definition: CPlusPlusNameParser.cpp:297
lldb_private::CPlusPlusNameParser::ParsedName
Definition: CPlusPlusNameParser.h:27
lldb_private::CPlusPlusNameParser::Range::size
size_t size() const
Definition: CPlusPlusNameParser.h:77
lldb_private::CPlusPlusNameParser::ConsumePtrsAndRefs
bool ConsumePtrsAndRefs()
Definition: CPlusPlusNameParser.cpp:459
lldb_private::CPlusPlusNameParser::Bookmark
Definition: CPlusPlusNameParser.h:89
lldb_private::CPlusPlusNameParser::SkipTypeQualifiers
void SkipTypeQualifiers()
Definition: CPlusPlusNameParser.cpp:408
lldb_private::CPlusPlusNameParser::ParseFunctionImpl
llvm::Optional< ParsedFunction > ParseFunctionImpl(bool expect_return_type)
Definition: CPlusPlusNameParser.cpp:105
lldb_private::CPlusPlusNameParser::ConsumeOperator
bool ConsumeOperator()
Definition: CPlusPlusNameParser.cpp:322
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::CPlusPlusNameParser::ConsumeBuiltinType
bool ConsumeBuiltinType()
Definition: CPlusPlusNameParser.cpp:418
ConstString.h
lldb_private::CPlusPlusNameParser::CPlusPlusNameParser
CPlusPlusNameParser(llvm::StringRef text)
Definition: CPlusPlusNameParser.h:25
lldb_private::CPlusPlusNameParser::ExtractTokens
void ExtractTokens()
Definition: CPlusPlusNameParser.cpp:673
lldb_private::CPlusPlusNameParser::Bookmark::Remove
void Remove()
Definition: CPlusPlusNameParser.h:102
lldb_private::CPlusPlusNameParser::SetBookmark
Bookmark SetBookmark()
Definition: CPlusPlusNameParser.cpp:93
lldb_private::CPlusPlusNameParser::ParseFullNameImpl
llvm::Optional< ParsedNameRanges > ParseFullNameImpl()
Definition: CPlusPlusNameParser.cpp:495
lldb_private::CPlusPlusNameParser::Bookmark::Bookmark
Bookmark(size_t &position)
Definition: CPlusPlusNameParser.h:91
lldb_private::CPlusPlusNameParser::m_text
llvm::StringRef m_text
Definition: CPlusPlusNameParser.h:61
lldb_private::CPlusPlusNameParser::Range::Range
Range()=default