[Lldb-commits] [lldb] r178477 - Proper option parser for the Sketch test case

Enrico Granata egranata at apple.com
Mon Apr 1 10:42:02 PDT 2013


Author: enrico
Date: Mon Apr  1 12:42:02 2013
New Revision: 178477

URL: http://llvm.org/viewvc/llvm-project?rev=178477&view=rev
Log:
Proper option parser for the Sketch test case

Modified:
    lldb/trunk/tools/lldb-perf/darwin/sketch/sketch.cpp

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=178477&r1=178476&r2=178477&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-perf/darwin/sketch/sketch.cpp (original)
+++ lldb/trunk/tools/lldb-perf/darwin/sketch/sketch.cpp Mon Apr  1 12:42:02 2013
@@ -18,6 +18,7 @@
 #include <iostream>
 #include <unistd.h>
 #include <fstream>
+#include <getopt.h>
 
 using namespace lldb_perf;
 
@@ -190,6 +191,52 @@ public:
         results.Write(m_out_path.c_str());
     }
     
+    void
+    SetExecutablePath (const char* str)
+    {
+        if (str)
+            m_app_path.assign(str);
+    }
+    
+    const char*
+    GetExecutablePath ()
+    {
+        if (m_app_path.empty())
+            return NULL;
+        return m_app_path.c_str();
+    }
+    
+    void
+    SetDocumentPath (const char* str)
+    {
+        if (str)
+            m_doc_path.assign(str);
+    }
+    
+    const char*
+    GetDocumentPath ()
+    {
+        if (m_doc_path.empty())
+            return NULL;
+        return m_doc_path.c_str();
+    }
+
+    
+    void
+    SetResultFilePath (const char* str)
+    {
+        if (str)
+            m_out_path.assign(str);
+    }
+    
+    const char*
+    GetResultFilePath ()
+    {
+        if (m_out_path.empty())
+            return NULL;
+        return m_out_path.c_str();
+    }
+    
 private:
     Measurement<lldb_perf::TimeGauge, std::function<void()>> m_fetch_frames_measurement;
     Measurement<lldb_perf::TimeGauge, std::function<void(const char*, uint32_t)>> m_file_line_bp_measurement;
@@ -202,13 +249,180 @@ private:
     std::string m_out_path;
 };
 
-// argv[1] == path to app
-// argv[2] == path to document
-// argv[3] == path to result
+struct Options
+{
+    std::string sketch_path;
+    std::string foobar_path;
+    std::string out_file;
+    bool verbose;
+    bool error;
+    bool print_help;
+    
+    Options() :
+    verbose (false),
+    error (false),
+    print_help (false)
+    {
+    }
+};
+
+static struct option g_long_options[] = {
+    { "verbose",    no_argument,            NULL, 'v' },
+    { "sketch",     required_argument,      NULL, 'c' },
+    { "foobar",     required_argument,      NULL, 'f' },
+    { "out-file",   required_argument,      NULL, 'o' },
+    { NULL,         0,                      NULL,  0  }
+};
+
+
+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;
+}
+
 int main(int argc, const char * argv[])
 {
-    SketchTest skt;
-    TestCase::Run(skt,argc,argv);
+    
+    // Prepare for & make calls to getopt_long.
+    
+    SketchTest test;
+    
+    std::string short_option_string (GetShortOptionString(g_long_options));
+    
+    Options option_data;
+    bool done = false;
+    
+#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(),
+                                                     g_long_options,
+                                                     &long_options_index);
+        
+        switch (short_option)
+        {
+            case 0:
+                // Already handled
+                break;
+                
+            case -1:
+                done = true;
+                break;
+                
+            case '?':
+                option_data.print_help = true;
+                break;
+                
+            case 'h':
+                option_data.print_help = true;
+                break;
+                
+            case 'v':
+                option_data.verbose = true;
+                break;
+                
+            case 'c':
+            {
+                SBFileSpec file(optarg);
+                if (file.Exists())
+                    test.SetExecutablePath(optarg);
+                else
+                    fprintf(stderr, "error: file specified in --sketch (-c) option doesn't exist: '%s'\n", optarg);
+            }
+                break;
+                
+            case 'f':
+            {
+                SBFileSpec file(optarg);
+                if (file.Exists())
+                    test.SetDocumentPath(optarg);
+                else
+                    fprintf(stderr, "error: file specified in --foobar (-f) option doesn't exist: '%s'\n", optarg);
+            }
+                break;
+                
+            case 'o':
+                test.SetResultFilePath(optarg);
+                break;
+                
+            default:
+                option_data.error = true;
+                option_data.print_help = true;
+                fprintf (stderr, "error: unrecognized option %c\n", short_option);
+                break;
+        }
+    }
+    
+    
+    if (test.GetExecutablePath() == NULL)
+    {
+        // --sketch is mandatory
+        option_data.print_help = true;
+        option_data.error = true;
+        fprintf (stderr, "error: the '--sketch=PATH' option is mandatory\n");
+    }
+    
+    if (test.GetDocumentPath() == NULL)
+    {
+        // --foobar is mandatory
+        option_data.print_help = true;
+        option_data.error = true;
+        fprintf (stderr, "error: the '--foobar=PATH' option is mandatory\n");
+    }
+    
+    if (option_data.print_help)
+    {
+        puts(R"(
+             NAME
+             lldb_perf_sketch -- a tool that measures LLDB peformance while debugging sketch.
+             
+             SYNOPSIS
+             lldb_perf_sketch --sketch=PATH --foobar=PATH [--out-file=PATH --verbose]
+             
+             DESCRIPTION
+             Runs a set of static timing and memory tasks against sketch and outputs results
+             to a plist file.
+             )");
+    }
+    if (option_data.error)
+    {
+        exit(1);
+    }
+    
+    // Update argc and argv after parsing options
+    argc -= optind;
+    argv += optind;
+    
+    test.SetVerbose(option_data.verbose);
+    TestCase::Run(test, argc, argv);
     return 0;
 }
-





More information about the lldb-commits mailing list