aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lldb-mi/MICmdArgSet.cpp
blob: 0d67c03bfeae8cb284d8c49a020cbce5bf5ed2f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//===-- MICmdArgSet.cpp -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// In-house headers:
#include "MICmdArgSet.h"
#include "MICmdArgValBase.h"
#include "MICmnLog.h"
#include "MICmnResources.h"

//++
// Details: CMICmdArgSet constructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdArgSet::CMICmdArgSet()
    : m_bIsArgsPresentButNotHandledByCmd(false), m_constStrCommaSpc(", ") {}

//++
// Details: CMICmdArgSet destructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdArgSet::~CMICmdArgSet() {
  // Tidy up
  Destroy();
}

//++
// Details: Release resources used by *this container object.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
void CMICmdArgSet::Destroy() {
  // Delete command argument objects
  if (!m_setCmdArgs.empty()) {
    SetCmdArgs_t::iterator it = m_setCmdArgs.begin();
    while (it != m_setCmdArgs.end()) {
      CMICmdArgValBase *pArg(*it);
      delete pArg;

      // Next
      ++it;
    }
    m_setCmdArgs.clear();
  }

  m_setCmdArgsThatNotValid.clear();
  m_setCmdArgsThatAreMissing.clear();
  m_setCmdArgsNotHandledByCmd.clear();
  m_setCmdArgsMissingInfo.clear();
  m_bIsArgsPresentButNotHandledByCmd = false;
}

//++
// Details: Retrieve the state flag indicating that the command set up ready to
// parse
//          command arguments or options found that one or more arguments was
//          indeed
//          present but not handled. This is given as a warning in the MI log
//          file.
// Type:    Method.
// Args:    None.
// Return:  bool - True = one or more args not handled, false = all args handled
// Throws:  None.
//--
bool CMICmdArgSet::IsArgsPresentButNotHandledByCmd() const {
  return m_bIsArgsPresentButNotHandledByCmd;
}

//++
// Details: Add the list of command's arguments to parse and validate another
// one.
// Type:    Method.
// Args:    vArg    - (R) A command argument object.
// Return:  None.
// Throws:  None.
//--
void CMICmdArgSet::Add(CMICmdArgValBase *vArg) { m_setCmdArgs.push_back(vArg); }

//++
// Details: After validating an options line of text (the context) and there is
// a failure,
//          it is likely a mandatory command argument that is required is
//          missing. This
//          function returns the argument that should be present.
// Type:    Method.
// Args:    None.
// Return:  SetCmdArgs_t & - Set of argument objects.
// Throws:  None.
//--
const CMICmdArgSet::SetCmdArgs_t &CMICmdArgSet::GetArgsThatAreMissing() const {
  return m_setCmdArgsThatAreMissing;
}

//++
// Details: After validating an options line of text (the context) and there is
// a failure,
//          it may be because one or more arguments were unable to extract a
//          value. This
//          function returns the argument that were found to be invalid.
// Type:    Method.
// Args:    None.
// Return:  SetCmdArgs_t & - Set of argument objects.
// Throws:  None.
//--
const CMICmdArgSet::SetCmdArgs_t &CMICmdArgSet::GetArgsThatInvalid() const {
  return m_setCmdArgsThatNotValid;
}

//++
// Details: The list of argument or option (objects) that were specified by the
// command
//          and so recognised when parsed but were not handled. Ideally the
//          command
//          should handle all arguments and options presented to it. The command
//          sends
//          warning to the MI log file to say that these options were not
//          handled.
//          Used as one way to determine option that maybe should really be
//          implemented
//          and not just ignored.
// Type:    Method.
// Args:    None.
// Return:  SetCmdArgs_t & - Set of argument objects.
// Throws:  None.
//--
const CMICmdArgSet::SetCmdArgs_t &CMICmdArgSet::GetArgsNotHandledByCmd() const {
  return m_setCmdArgsNotHandledByCmd;
}

//++
// Details: Given a set of command argument objects parse the context option
// string to
//          find those argument and retrieve their value. If the function fails
//          call
//          GetArgsThatAreMissing() to see which commands that were mandatory
//          were
//          missing or failed to parse.
// Type:    Method.
// Args:    vStrMiCmd       - (R)  Command's name.
//          vCmdArgsText    - (RW) A command's options or argument.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool CMICmdArgSet::Validate(const CMIUtilString &vStrMiCmd,
                            CMICmdArgContext &vwCmdArgsText) {
  m_cmdArgContext = vwCmdArgsText;

  // Iterate all the arguments or options required by a command
  SetCmdArgs_t::const_iterator it = m_setCmdArgs.begin();
  while (it != m_setCmdArgs.end()) {
    CMICmdArgValBase *pArg = *it;

    if (!pArg->Validate(vwCmdArgsText)) {
      if (pArg->GetFound()) {
        if (pArg->GetIsMissingOptions())
          m_setCmdArgsMissingInfo.push_back(pArg);
        else if (!pArg->GetValid())
          m_setCmdArgsThatNotValid.push_back(pArg);
      } else if (pArg->GetIsMandatory())
        m_setCmdArgsThatAreMissing.push_back(pArg);
    }

    if (pArg->GetFound() && !pArg->GetIsHandledByCmd()) {
      m_bIsArgsPresentButNotHandledByCmd = true;
      m_setCmdArgsNotHandledByCmd.push_back(pArg);
    }

    // Next
    ++it;
  }

  // report any issues with arguments/options
  if (IsArgsPresentButNotHandledByCmd())
    WarningArgsNotHandledbyCmdLogFile(vStrMiCmd);

  return ValidationFormErrorMessages(vwCmdArgsText);
}

//++
// Details: Having validated the command's options text and failed for some
// reason form
//          the error message made up with the faults found.
// Type:    Method.
//          vCmdArgsText    - (RW) A command's options or argument.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool CMICmdArgSet::ValidationFormErrorMessages(
    const CMICmdArgContext &vwCmdArgsText) {
  CMIUtilString strListMissing;
  CMIUtilString strListInvalid;
  CMIUtilString strListMissingInfo;
  const bool bArgsMissing = (m_setCmdArgsThatAreMissing.size() > 0);
  const bool bArgsInvalid = (m_setCmdArgsThatNotValid.size() > 0);
  const bool bArgsMissingInfo = (m_setCmdArgsMissingInfo.size() > 0);
  if (!(bArgsMissing || bArgsInvalid || bArgsMissingInfo))
    return MIstatus::success;
  if (bArgsMissing) {
    MIuint i = 0;
    SetCmdArgs_t::const_iterator it = m_setCmdArgsThatAreMissing.begin();
    while (it != m_setCmdArgsThatAreMissing.end()) {
      if (i++ > 0)
        strListMissing += m_constStrCommaSpc;

      const CMICmdArgValBase *pArg(*it);
      strListMissing += pArg->GetName();

      // Next
      ++it;
    }
  }
  if (bArgsInvalid) {
    MIuint i = 0;
    SetCmdArgs_t::const_iterator it = m_setCmdArgsThatNotValid.begin();
    while (it != m_setCmdArgsThatNotValid.end()) {
      if (i++ > 0)
        strListMissing += m_constStrCommaSpc;

      const CMICmdArgValBase *pArg(*it);
      strListInvalid += pArg->GetName();

      // Next
      ++it;
    }
  }
  if (bArgsMissingInfo) {
    MIuint i = 0;
    SetCmdArgs_t::const_iterator it = m_setCmdArgsMissingInfo.begin();
    while (it != m_setCmdArgsMissingInfo.end()) {
      if (i++ > 0)
        strListMissingInfo += m_constStrCommaSpc;

      const CMICmdArgValBase *pArg(*it);
      strListMissingInfo += pArg->GetName();

      // Next
      ++it;
    }
  }

  bool bHaveOneError = false;
  CMIUtilString strError = MIRSRC(IDS_CMD_ARGS_ERR_PREFIX_MSG);
  if (bArgsMissing && bArgsInvalid) {
    bHaveOneError = true;
    strError +=
        CMIUtilString::Format(MIRSRC(IDS_CMD_ARGS_ERR_VALIDATION_MAN_INVALID),
                              strListMissing.c_str(), strListInvalid.c_str());
  }
  if (bArgsMissing) {
    if (bHaveOneError)
      strError += ". ";
    bHaveOneError = true;
    strError += CMIUtilString::Format(
        MIRSRC(IDS_CMD_ARGS_ERR_VALIDATION_MANDATORY), strListMissing.c_str());
  }
  if (bArgsMissingInfo) {
    if (bHaveOneError)
      strError += ". ";
    bHaveOneError = true;
    strError +=
        CMIUtilString::Format(MIRSRC(IDS_CMD_ARGS_ERR_VALIDATION_MISSING_INF),
                              strListMissingInfo.c_str());
  }
  if (bArgsInvalid) {
    if (bHaveOneError)
      strError += ". ";
    bHaveOneError = true;
    strError += CMIUtilString::Format(
        MIRSRC(IDS_CMD_ARGS_ERR_VALIDATION_INVALID), strListInvalid.c_str());
  }
  if (!vwCmdArgsText.IsEmpty()) {
    if (bHaveOneError)
      strError += ". ";
    bHaveOneError = true;
    strError +=
        CMIUtilString::Format(MIRSRC(IDS_CMD_ARGS_ERR_CONTEXT_NOT_ALL_EATTEN),
                              vwCmdArgsText.GetArgsLeftToParse().c_str());
  }

  if (bHaveOneError) {
    SetErrorDescription(strError);
    return MIstatus::failure;
  }

  return MIstatus::success;
}

//++
// Details: Ask if the command's argument options text had any arguments.
// Type:    Method.
// Args:    None.
// Return:  bool    - True = Has one or more arguments present, false = no
// arguments.
// Throws:  None.
//--
bool CMICmdArgSet::IsArgContextEmpty() const {
  return m_cmdArgContext.IsEmpty();
}

//++
// Details: Retrieve the number of arguments that are being used for the
// command.
// Type:    Method.
// Args:    None.
// Return:  size_t - Argument count.
// Throws:  None.
//--
size_t CMICmdArgSet::GetCount() const { return m_setCmdArgs.size(); }

//++
// Details: Given a set of command argument objects retrieve the argument with
// the
//          specified name.
// Type:    Method.
// Args:    vpArg   - (W) A pointer to a command's argument object.
// Return:  True - Argument found.
//          False - Argument not found.
// Throws:  None.
//--
bool CMICmdArgSet::GetArg(const CMIUtilString &vArgName,
                          CMICmdArgValBase *&vpArg) const {
  bool bFound = false;
  SetCmdArgs_t::const_iterator it = m_setCmdArgs.begin();
  while (it != m_setCmdArgs.end()) {
    CMICmdArgValBase *pArg(*it);
    if (pArg->GetName() == vArgName) {
      bFound = true;
      vpArg = pArg;
      break;
    }

    // Next
    ++it;
  }

  return bFound;
}

//++
// Details: Write a warning message to the MI Log file about the command's
// arguments or
//          options that were found present but not handled.
// Type:    Method.
// Args:    vrCmdName   - (R) The command's name.
// Return:  None.
// Throws:  None.
//--
void CMICmdArgSet::WarningArgsNotHandledbyCmdLogFile(
    const CMIUtilString &vrCmdName) {
#if MICONFIG_GIVE_WARNING_CMD_ARGS_NOT_HANDLED

  CMIUtilString strArgsNotHandled;
  const CMICmdArgSet::SetCmdArgs_t &rSetArgs = GetArgsNotHandledByCmd();
  MIuint nCnt = 0;
  CMICmdArgSet::SetCmdArgs_t::const_iterator it = rSetArgs.begin();
  while (it != rSetArgs.end()) {
    if (nCnt++ > 0)
      strArgsNotHandled += m_constStrCommaSpc;
    const CMICmdArgValBase *pArg = *it;
    strArgsNotHandled += pArg->GetName();

    // Next
    ++it;
  }

  const CMIUtilString strWarningMsg(
      CMIUtilString::Format(MIRSRC(IDS_CMD_WRN_ARGS_NOT_HANDLED),
                            vrCmdName.c_str(), strArgsNotHandled.c_str()));
  m_pLog->WriteLog(strWarningMsg);

#endif // MICONFIG_GIVE_WARNING_CMD_ARGS_NOT_HANDLED
}