[Lldb-commits] [lldb] r178482 - Integrating option parsing in TestCase for added convenience
Enrico Granata
egranata at apple.com
Mon Apr 1 11:02:25 PDT 2013
Author: enrico
Date: Mon Apr 1 13:02:25 2013
New Revision: 178482
URL: http://llvm.org/viewvc/llvm-project?rev=178482&view=rev
Log:
Integrating option parsing in TestCase for added convenience
To hook it up to individual test cases:
- define GetLongOptions() in your test case class to return something other than NULL (hopefully an array of options :-)
- implement ParseOption() to check for the short option char and do the right thing - return true at the end if you want more options to come your way or false if you don’t
- make sure that your Setup() call takes int& and char**& so that optind post-processing can happen - and call TestCase::Setup from your setup
Modified:
lldb/trunk/tools/lldb-perf/common/clang/lldb_perf_clang.cpp
lldb/trunk/tools/lldb-perf/common/stepping/lldb-perf-stepping.cpp
lldb/trunk/tools/lldb-perf/darwin/formatters/formatters.cpp
lldb/trunk/tools/lldb-perf/darwin/sketch/sketch.cpp
lldb/trunk/tools/lldb-perf/lib/TestCase.cpp
lldb/trunk/tools/lldb-perf/lib/TestCase.h
Modified: lldb/trunk/tools/lldb-perf/common/clang/lldb_perf_clang.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-perf/common/clang/lldb_perf_clang.cpp?rev=178482&r1=178481&r2=178482&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-perf/common/clang/lldb_perf_clang.cpp (original)
+++ lldb/trunk/tools/lldb-perf/common/clang/lldb_perf_clang.cpp Mon Apr 1 13:02:25 2013
@@ -68,7 +68,7 @@ public:
}
virtual bool
- Setup (int argc, const char** argv)
+ Setup (int& argc, const char**& argv)
{
if (m_exe_path.empty())
return false;
Modified: lldb/trunk/tools/lldb-perf/common/stepping/lldb-perf-stepping.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-perf/common/stepping/lldb-perf-stepping.cpp?rev=178482&r1=178481&r2=178482&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-perf/common/stepping/lldb-perf-stepping.cpp (original)
+++ lldb/trunk/tools/lldb-perf/common/stepping/lldb-perf-stepping.cpp Mon Apr 1 13:02:25 2013
@@ -28,7 +28,7 @@ public:
~StepTest() {}
virtual bool
- Setup (int argc, const char **argv)
+ Setup (int& argc, const char**& argv)
{
TestCase::Setup (argc, argv);
Modified: lldb/trunk/tools/lldb-perf/darwin/formatters/formatters.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-perf/darwin/formatters/formatters.cpp?rev=178482&r1=178481&r2=178482&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-perf/darwin/formatters/formatters.cpp (original)
+++ lldb/trunk/tools/lldb-perf/darwin/formatters/formatters.cpp Mon Apr 1 13:02:25 2013
@@ -71,7 +71,7 @@ public:
}
virtual bool
- Setup (int argc, const char** argv)
+ Setup (int& argc, const char**& argv)
{
m_app_path.assign(argv[1]);
m_out_path.assign(argv[2]);
Modified: lldb/trunk/tools/lldb-perf/darwin/sketch/sketch.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-perf/darwin/sketch/sketch.cpp?rev=178482&r1=178481&r2=178482&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-perf/darwin/sketch/sketch.cpp (original)
+++ lldb/trunk/tools/lldb-perf/darwin/sketch/sketch.cpp Mon Apr 1 13:02:25 2013
@@ -62,7 +62,7 @@ public:
}
virtual bool
- Setup (int argc, const char** argv)
+ Setup (int& argc, const char**& argv)
{
//SetVerbose(true);
m_app_path.assign(argv[1]);
Modified: lldb/trunk/tools/lldb-perf/lib/TestCase.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-perf/lib/TestCase.cpp?rev=178482&r1=178481&r2=178482&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-perf/lib/TestCase.cpp (original)
+++ lldb/trunk/tools/lldb-perf/lib/TestCase.cpp Mon Apr 1 13:02:25 2013
@@ -28,9 +28,77 @@ TestCase::TestCase () :
m_listener = m_debugger.GetListener();
}
+static std::string
+GetShortOptionString (struct option *long_options)
+{
+ std::string option_string;
+ for (int i = 0; long_options[i].name != NULL; ++i)
+ {
+ if (long_options[i].flag == NULL)
+ {
+ option_string.push_back ((char) long_options[i].val);
+ switch (long_options[i].has_arg)
+ {
+ default:
+ case no_argument:
+ break;
+ case required_argument:
+ option_string.push_back (':');
+ break;
+ case optional_argument:
+ option_string.append (2, ':');
+ break;
+ }
+ }
+ }
+ return option_string;
+}
+
bool
-TestCase::Setup (int argc, const char** argv)
+TestCase::Setup (int& argc, const char**& argv)
{
+ bool done = false;
+
+ struct option* long_options = GetLongOptions();
+
+ if (long_options)
+ {
+ std::string short_option_string (GetShortOptionString(long_options));
+
+ #if __GLIBC__
+ optind = 0;
+ #else
+ optreset = 1;
+ optind = 1;
+ #endif
+ while (!done)
+ {
+ int long_options_index = -1;
+ const int short_option = ::getopt_long_only (argc,
+ const_cast<char **>(argv),
+ short_option_string.c_str(),
+ long_options,
+ &long_options_index);
+
+ switch (short_option)
+ {
+ case 0:
+ // Already handled
+ break;
+
+ case -1:
+ done = true;
+ break;
+
+ default:
+ done = !ParseOption(short_option, optarg);
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ }
+
return false;
}
Modified: lldb/trunk/tools/lldb-perf/lib/TestCase.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-perf/lib/TestCase.h?rev=178482&r1=178481&r2=178482&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-perf/lib/TestCase.h (original)
+++ lldb/trunk/tools/lldb-perf/lib/TestCase.h Mon Apr 1 13:02:25 2013
@@ -12,6 +12,7 @@
#include "lldb/API/LLDB.h"
#include "Measurement.h"
+#include <getopt.h>
namespace lldb_perf {
@@ -74,7 +75,7 @@ public:
}
virtual bool
- Setup (int argc, const char** argv);
+ Setup (int& argc, const char**& argv);
virtual void
TestStep (int counter, ActionWanted &next_action) = 0;
@@ -115,6 +116,18 @@ public:
static void
Run (TestCase& test, int argc, const char** argv);
+ virtual bool
+ ParseOption (int short_option, const char* optarg)
+ {
+ return false;
+ }
+
+ virtual struct option*
+ GetLongOptions ()
+ {
+ return NULL;
+ }
+
lldb::SBDebugger &
GetDebugger()
{
More information about the lldb-commits
mailing list