]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdArgValOptionLong.cpp
Update llvm, clang and lldb to 3.7.0 release.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdArgValOptionLong.cpp
1 //===-- MICmdArgValOptionLong.cpp -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // In-house headers:
11 #include "MICmdArgValOptionLong.h"
12 #include "MICmdArgContext.h"
13
14 //++ ------------------------------------------------------------------------------------
15 // Details: CMICmdArgValOptionLong constructor.
16 // Type:    Method.
17 // Args:    None.
18 // Return:  None.
19 // Throws:  None.
20 //--
21 CMICmdArgValOptionLong::CMICmdArgValOptionLong(void)
22     : m_nExpectingNOptions(0)
23     , m_eExpectingOptionType(eArgValType_invalid)
24 {
25 }
26
27 //++ ------------------------------------------------------------------------------------
28 // Details: CMICmdArgValOptionLong constructor.
29 // Type:    Method.
30 // Args:    vrArgName       - (R) Argument's name to search by.
31 //          vbMandatory     - (R) True = Yes must be present, false = optional argument.
32 //          vbHandleByCmd   - (R) True = Command processes *this option, false = not handled.
33 // Return:  None.
34 // Throws:  None.
35 //--
36 CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd)
37     : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd)
38     , m_nExpectingNOptions(0)
39     , m_eExpectingOptionType(eArgValType_invalid)
40 {
41 }
42
43 //++ ------------------------------------------------------------------------------------
44 // Details: CMICmdArgValOptionLong constructor.
45 // Type:    Method.
46 // Args:    vrArgName           - (R) Argument's name to search by.
47 //          vbMandatory         - (R) True = Yes must be present, false = optional argument.
48 //          vbHandleByCmd       - (R) True = Command processes *this option, false = not handled.
49 //          veType              - (R) The type of argument to look for and create argument object of a certain type.
50 //          vnExpectingNOptions - (R) The number of options expected to read following *this argument.
51 // Return:  None.
52 // Throws:  None.
53 //--
54 CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd,
55                                                const ArgValType_e veType, const MIuint vnExpectingNOptions)
56     : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd)
57     , m_nExpectingNOptions(vnExpectingNOptions)
58     , m_eExpectingOptionType(veType)
59 {
60 }
61
62 //++ ------------------------------------------------------------------------------------
63 // Details: CMICmdArgValOptionLong destructor.
64 // Type:    Overridden.
65 // Args:    None.
66 // Return:  None.
67 // Throws:  None.
68 //--
69 CMICmdArgValOptionLong::~CMICmdArgValOptionLong(void)
70 {
71     // Tidy up
72     Destroy();
73 }
74
75 //++ ------------------------------------------------------------------------------------
76 // Details: Tear down resources used by *this object.
77 // Type:    Method.
78 // Args:    None.
79 // Return:  None.
80 // Throws:  None.
81 //--
82 void
83 CMICmdArgValOptionLong::Destroy(void)
84 {
85     // Tidy up
86     VecArgObjPtr_t::const_iterator it = m_vecArgsExpected.begin();
87     while (it != m_vecArgsExpected.end())
88     {
89         CMICmdArgValBase *pOptionObj = *it;
90         delete pOptionObj;
91
92         // Next
93         ++it;
94     }
95     m_vecArgsExpected.clear();
96 }
97
98 //++ ------------------------------------------------------------------------------------
99 // Details: Parse the command's argument options string and try to extract the long
100 //          argument *this argument type is looking for.
101 // Type:    Overridden.
102 // Args:    vwArgContext    - (RW) The command's argument options string.
103 // Return:  MIstatus::success - Functional succeeded.
104 //          MIstatus::failure - Functional failed.
105 // Throws:  None.
106 //--
107 bool
108 CMICmdArgValOptionLong::Validate(CMICmdArgContext &vwArgContext)
109 {
110     if (vwArgContext.IsEmpty())
111         return m_bMandatory ? MIstatus::failure : MIstatus::success;
112
113     if (vwArgContext.GetNumberArgsPresent() == 1)
114     {
115         const CMIUtilString &rArg(vwArgContext.GetArgsLeftToParse());
116         if (IsArgLongOption(rArg) && ArgNameMatch(rArg))
117         {
118             m_bFound = true;
119
120             if (!vwArgContext.RemoveArg(rArg))
121                 return MIstatus::failure;
122
123             if (m_nExpectingNOptions == 0)
124             {
125                 m_bValid = true;
126                 return MIstatus::success;
127             }
128
129             m_bIsMissingOptions = true;
130             return MIstatus::failure;
131         }
132         else
133             return MIstatus::failure;
134     }
135
136     // More than one option...
137     MIuint nArgIndex = 0;
138     const CMIUtilString::VecString_t vecOptions(vwArgContext.GetArgs());
139     CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
140     while (it != vecOptions.end())
141     {
142         const CMIUtilString &rArg(*it);
143         if (IsArgOptionCorrect(rArg) && ArgNameMatch(rArg))
144         {
145             m_bFound = true;
146
147             if (!vwArgContext.RemoveArg(rArg))
148                 return MIstatus::failure;
149
150             if (m_nExpectingNOptions != 0)
151             {
152                 if (ExtractExpectedOptions(vwArgContext, nArgIndex))
153                 {
154                     m_bValid = true;
155                     return MIstatus::success;
156                 }
157
158                 m_bIsMissingOptions = true;
159                 return MIstatus::failure;
160             }
161             else
162             {
163                 m_bValid = true;
164                 return MIstatus::success;
165             }
166         }
167
168         // Next
169         ++it;
170         ++nArgIndex;
171     }
172
173     return MIstatus::failure;
174 }
175
176 //++ ------------------------------------------------------------------------------------
177 // Details: Parse the text following *this argument and extract the options the values of
178 //          CMICmdArgValListBase::m_eArgType forming argument objects for each of those
179 //          options extracted.
180 // Type:    Method.
181 // Args:    vrwTxt      - (RW)  The command's argument options string.
182 //          nArgIndex   - (R)   The Nth arg position in argument context from the left.
183 // Return:  MIstatus::success - Functional succeeded.
184 //          MIstatus::failure - Functional failed.
185 // Throws:  None.
186 //--
187 bool
188 CMICmdArgValOptionLong::ExtractExpectedOptions(CMICmdArgContext &vrwTxt, const MIuint nArgIndex)
189 {
190     CMIUtilString::VecString_t vecOptions;
191     MIuint nOptionsPresent = 0;
192     if ((m_eExpectingOptionType != eArgValType_StringQuoted) && (m_eExpectingOptionType != eArgValType_StringQuotedNumber) &&
193         (m_eExpectingOptionType != eArgValType_StringQuotedNumberPath))
194         nOptionsPresent = vrwTxt.GetArgsLeftToParse().Split(" ", vecOptions);
195     else
196         nOptionsPresent = vrwTxt.GetArgsLeftToParse().SplitConsiderQuotes(" ", vecOptions);
197     if (nOptionsPresent == 0)
198         return MIstatus::failure;
199
200     MIuint nArgIndexCnt = 0;
201     MIuint nTypeCnt = 0;
202     MIuint nTypeCnt2 = 0;
203     MIuint nFoundNOptionsCnt = 0;
204     CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
205     while (it != vecOptions.end())
206     {
207         // Move to the Nth argument position from left before do validation/checking
208         if (nArgIndexCnt++ == nArgIndex)
209         {
210             nTypeCnt++;
211             const CMIUtilString &rOption(*it);
212             if (IsExpectedCorrectType(rOption, m_eExpectingOptionType))
213             {
214                 nTypeCnt2++;
215                 CMICmdArgValBase *pOptionObj = CreationObj(rOption, m_eExpectingOptionType);
216                 if ((pOptionObj != nullptr) && vrwTxt.RemoveArgAtPos(rOption, nArgIndex))
217                 {
218                     nFoundNOptionsCnt++;
219                     m_vecArgsExpected.push_back(pOptionObj);
220                 }
221             }
222
223             // Is the sequence 'options' of same type broken. Expecting the same type until the
224             // next argument.
225             if (nTypeCnt != nTypeCnt2)
226                 return MIstatus::failure;
227
228             if (nFoundNOptionsCnt == m_nExpectingNOptions)
229                 return MIstatus::success;
230         }
231
232         // Next
233         ++it;
234     }
235     if (nFoundNOptionsCnt != m_nExpectingNOptions)
236         return MIstatus::failure;
237
238     return MIstatus::success;
239 }
240
241 //++ ------------------------------------------------------------------------------------
242 // Details: Examine the string and determine if it is a valid long type option argument.
243 //          Long type argument looks like --someLongOption.
244 // Type:    Method.
245 // Args:    vrTxt   - (R) Some text.
246 // Return:  bool    - True = yes valid arg, false = no.
247 // Throws:  None.
248 //--
249 bool
250 CMICmdArgValOptionLong::IsArgLongOption(const CMIUtilString &vrTxt) const
251 {
252     const bool bHavePosSlash = (vrTxt.find_first_of("/") != std::string::npos);
253     const bool bHaveBckSlash = (vrTxt.find_first_of("\\") != std::string::npos);
254     if (bHavePosSlash || bHaveBckSlash)
255         return false;
256
257     const size_t nPos = vrTxt.find_first_of("--");
258     if (nPos != 0)
259         return false;
260
261     if (vrTxt.length() < 3)
262         return false;
263
264     const CMIUtilString strArg = vrTxt.substr(2).c_str();
265     if (strArg.IsNumber())
266         return false;
267
268     return true;
269 }
270
271 //++ ------------------------------------------------------------------------------------
272 // Details: Examine the string and determine if it is a valid long type option argument.
273 //          Long type argument looks like --someLongOption.
274 // Type:    Overideable.
275 // Args:    vrTxt   - (R) Some text.
276 // Return:  bool    - True = yes valid arg, false = no.
277 // Throws:  None.
278 //--
279 bool
280 CMICmdArgValOptionLong::IsArgOptionCorrect(const CMIUtilString &vrTxt) const
281 {
282     return IsArgLongOption(vrTxt);
283 }
284
285 //++ ------------------------------------------------------------------------------------
286 // Details: Does the argument name of the argument being parsed ATM match the name of
287 //          *this argument object.
288 // Type:    Method.
289 // Args:    vrTxt   - (R) Some text.
290 // Return:  bool    - True = yes arg name matched, false = no.
291 // Throws:  None.
292 //--
293 bool
294 CMICmdArgValOptionLong::ArgNameMatch(const CMIUtilString &vrTxt) const
295 {
296     const CMIUtilString strArg = vrTxt.substr(2).c_str();
297     return (strArg == GetName());
298 }
299
300 //++ ------------------------------------------------------------------------------------
301 // Details: Retrieve the list of CMICmdArgValBase derived option objects found following
302 //          *this long option argument. For example "list-thread-groups [ --recurse 1 ]"
303 //          where 1 is the list of expected option to follow.
304 // Type:    Method.
305 // Args:    None.
306 // Return:  CMICmdArgValListBase::VecArgObjPtr_t & - List of options.
307 // Throws:  None.
308 //--
309 const CMICmdArgValListBase::VecArgObjPtr_t &
310 CMICmdArgValOptionLong::GetExpectedOptions(void) const
311 {
312     return m_vecArgsExpected;
313 }