Constants never get names.
[oota-llvm.git] / docs / CommandLine.html
index 8c59b2b3716d5faf1532b05d4ccd327ca7b80a1d..da28beb3840d8a74da64863e9be78614dbe9c2de 100644 (file)
@@ -2,6 +2,7 @@
                       "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>CommandLine 2.0 Library Manual</title>
   <link rel="stylesheet" href="llvm.css" type="text/css">
 </head>
@@ -30,6 +31,8 @@
       <li><a href="#positional">Positional Arguments</a>
         <ul>
         <li><a href="#--">Specifying positional options with hyphens</a></li>
+        <li><a href="#getPosition">Determining absolute position with
+          getPosition</a></li>
         <li><a href="#cl::ConsumeAfter">The <tt>cl::ConsumeAfter</tt>
              modifier</a></li>
         </ul></li>
@@ -59,6 +62,7 @@
         <li><a href="#cl::opt">The <tt>cl::opt</tt> class</a></li>
         <li><a href="#cl::list">The <tt>cl::list</tt> class</a></li>
         <li><a href="#cl::alias">The <tt>cl::alias</tt> class</a></li>
+        <li><a href="#cl::extrahelp">The <tt>cl::extrahelp</tt> class</a></li>
         </ul></li>
 
       <li><a href="#builtinparsers">Builtin parsers</a>
@@ -84,8 +88,8 @@
     </ol></li>
 </ol>
 
-<div class="doc_text">
-  <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b></p>
+<div class="doc_author">
+  <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
 </div>
 
 <!-- *********************************************************************** -->
@@ -215,10 +219,10 @@ we would like to support the unix standard '<tt>-o &lt;filename&gt;</tt>' option
 to specify where to put the output.  With the CommandLine library, this is
 represented like this:</p>
 
-<p><tt>
-<a name="value_desc_example">
-<a href="#cl::opt">cl::opt</a>&lt;string&gt; OutputFilename("<i>o</i>", <a href="#cl::desc">cl::desc</a>("<i>Specify output filename</i>"), <a href="#cl::value_desc">cl::value_desc</a>("<i>filename</i>"));</a>
-</tt></p>
+<a name="value_desc_example"></a>
+<pre>
+<a href="#cl::opt">cl::opt</a>&lt;string&gt; OutputFilename("<i>o</i>", <a href="#cl::desc">cl::desc</a>("<i>Specify output filename</i>"), <a href="#cl::value_desc">cl::value_desc</a>("<i>filename</i>"));
+</pre>
 
 <p>This declares a global variable "<tt>OutputFilename</tt>" that is used to
 capture the result of the "<tt>o</tt>" argument (first parameter).  We specify
@@ -458,10 +462,10 @@ things it doesn't know about, like enums or '<tt>int*</tt>'s?</p>
 
 <p>The answer is that it uses a table driven generic parser (unless you specify
 your own parser, as described in the <a href="#extensionguide">Extension
-Guide</a>).  This parser maps literal strings to whatever type is required, are
+Guide</a>).  This parser maps literal strings to whatever type is required, and
 requires you to tell it what this mapping should be.</p>
 
-<p>Lets say that we would like to add four optimizations levels to our
+<p>Lets say that we would like to add four optimization levels to our
 optimizer, using the standard flags "<tt>-g</tt>", "<tt>-O0</tt>",
 "<tt>-O1</tt>", and "<tt>-O2</tt>".  We could easily implement this with boolean
 options like above, but there are several problems with this strategy:</p>
@@ -493,7 +497,7 @@ enum OptLevel {
     clEnumVal(O1, "<i>Enable trivial optimizations</i>"),
     clEnumVal(O2, "<i>Enable default optimizations</i>"),
     clEnumVal(O3, "<i>Enable expensive optimizations</i>"),
-   0));
+   clEnumValEnd));
 
 ...
   if (OptimizationLevel &gt;= O2) doPartialRedundancyElimination(...);
@@ -503,7 +507,8 @@ enum OptLevel {
 <p>This declaration defines a variable "<tt>OptimizationLevel</tt>" of the
 "<tt>OptLevel</tt>" enum type.  This variable can be assigned any of the values
 that are listed in the declaration (Note that the declaration list must be
-terminated with the "<tt>0</tt>" argument!).  The CommandLine library enforces
+terminated with the "<tt>clEnumValEnd</tt>" argument!).  The CommandLine 
+library enforces
 that the user can only specify one of the options, and it ensure that only valid
 enum values can be specified.  The "<tt>clEnumVal</tt>" macros ensure that the
 command line arguments matched the enum values.  With this option added, our
@@ -540,7 +545,7 @@ enum OptLevel {
     clEnumVal(O1        , "<i>Enable trivial optimizations</i>"),
     clEnumVal(O2        , "<i>Enable default optimizations</i>"),
     clEnumVal(O3        , "<i>Enable expensive optimizations</i>"),
-   0));
+   clEnumValEnd));
 
 ...
   if (OptimizationLevel == Debug) outputDebugInfo(...);
@@ -581,7 +586,7 @@ enum DebugLev {
     clEnumValN(nodebuginfo, "none", "<i>disable debug information</i>"),
      clEnumVal(quick,               "<i>enable quick debug information</i>"),
      clEnumVal(detailed,            "<i>enable detailed debug information</i>"),
-    0));
+    clEnumValEnd));
 </pre>
 
 <p>This definition defines an enumerated command line variable of type "<tt>enum
@@ -609,7 +614,7 @@ OPTIONS:
 </pre>
 
 <p>Again, the only structural difference between the debug level declaration and
-the optimiation level declaration is that the debug level declaration includes
+the optimization level declaration is that the debug level declaration includes
 an option name (<tt>"debug_level"</tt>), which automatically changes how the
 library processes the argument.  The CommandLine library supports both forms so
 that you can choose the form most appropriate for your application.</p>
@@ -648,7 +653,7 @@ enum Opts {
     clEnumVal(constprop         , "<i>Constant Propagation</i>"),
    clEnumValN(inlining, "<i>inline</i>", "<i>Procedure Integration</i>"),
     clEnumVal(strip             , "<i>Strip Symbols</i>"),
-  0));
+  clEnumValEnd));
 </pre>
 
 <p>This defines a variable that is conceptually of the type
@@ -779,7 +784,7 @@ OPTIONS:
 
 <p>Positional arguments are sorted by their order of construction.  This means
 that command line options will be ordered according to how they are listed in a
-.cpp file, but will not have an ordering defined if they positional arguments
+.cpp file, but will not have an ordering defined if the positional arguments
 are defined in multiple .cpp files.  The fix for this problem is simply to
 define all of your positional arguments in one .cpp file.</p>
 
@@ -823,6 +828,63 @@ can use it like this:</p>
 
 </div>
 
+<!-- _______________________________________________________________________ -->
+<div class="doc_subsubsection">
+  <a name="getPosition">Determining absolute position with getPosition()</a>
+</div>
+<div class="doc_text">
+  <p>Sometimes an option can affect or modify the meaning of another option. For
+  example, consider <tt>gcc</tt>'s <tt>-x LANG</tt> option. This tells
+  <tt>gcc</tt> to ignore the suffix of subsequent positional arguments and force
+  the file to be interpreted as if it contained source code in language
+  <tt>LANG</tt>. In order to handle this properly , you need to know the 
+  absolute position of each argument, especially those in lists, so their 
+  interaction(s) can be applied correctly. This is also useful for options like 
+  <tt>-llibname</tt> which is actually a positional argument that starts with 
+  a dash.</p>
+  <p>So, generally, the problem is that you have two <tt>cl::list</tt> variables
+  that interact in some way. To ensure the correct interaction, you can use the
+  <tt>cl::list::getPosition(optnum)</tt> method. This method returns the
+  absolute position (as found on the command line) of the <tt>optnum</tt>
+  item in the <tt>cl::list</tt>.</p>
+  <p>The idiom for usage is like this:<pre><tt>
+  static cl::list&lt;std::string&gt; Files(cl::Positional, cl::OneOrMore);
+  static cl::listlt;std::string&gt; Libraries("l", cl::ZeroOrMore);
+
+  int main(int argc, char**argv) {
+    // ...
+    std::vector&lt;std::string&gt;::iterator fileIt = Files.begin();
+    std::vector&lt;std::string&gt;::iterator libIt  = Libraries.begin();
+    unsigned libPos = 0, filePos = 0;
+    while ( 1 ) {
+      if ( libIt != Libraries.end() )
+        libPos = Libraries.getPosition( libIt - Libraries.begin() );
+      else
+        libPos = 0;
+      if ( fileIt != Files.end() )
+        filePos = Files.getPosition( fileIt - Files.begin() );
+      else
+        filePos = 0;
+
+      if ( filePos != 0 &amp;&amp; (libPos == 0 || filePos &lt; libPos) ) {
+        // Source File Is next
+        ++fileIt;
+      }
+      else if ( libPos != 0 &amp;&amp; (filePos == 0 || libPos &lt; filePos) ) {
+        // Library is next
+        ++libIt;
+      }
+      else
+        break; // we're done with the list
+    }
+  }
+  </tt></pre></p>
+  <p>Note that, for compatibility reasons, the <tt>cl::opt</tt> also supports an
+  <tt>unsigned getPosition()</tt> option that will provide the absolute position
+  of that option. You can apply the same approach as above with a 
+  <tt>cl::opt</tt> and a <tt>cl::list</tt> option as you can with two lists.</p>
+</div>
+
 <!-- _______________________________________________________________________ -->
 <div class="doc_subsubsection">
   <a name="cl::ConsumeAfter">The <tt>cl::ConsumeAfter</tt> modifier</a>
@@ -870,13 +932,14 @@ name).</p>
 <p>There are several limitations to when <tt>cl::ConsumeAfter</tt> options can
 be specified.  For example, only one <tt>cl::ConsumeAfter</tt> can be specified
 per program, there must be at least one <a href="#positional">positional
-argument</a> specified, and the <tt>cl::ConsumeAfter</tt> option should be a <a
+argument</a> specified, there must not be any <a href="#cl::list">cl::list</a>
+positional arguments, and the <tt>cl::ConsumeAfter</tt> option should be a <a
 href="#cl::list">cl::list</a> option.</p>
 
 </div>
 
 <!-- ======================================================================= -->
-<div class="subsection">
+<div class="doc_subsection">
   <a name="storage">Internal vs External Storage</a>
 </div>
 
@@ -898,6 +961,7 @@ all of these clients (requiring lots of .cpp files to #include
 
 <p>To do this, set up your .h file with your option, like this for example:</p>
 
+<div class="doc_code">
 <pre>
 <i>// DebugFlag.h - Get access to the '-debug' command line option
 //
@@ -913,15 +977,15 @@ extern bool DebugFlag;
 // debug build, then the code specified as the option to the macro will be
 // executed.  Otherwise it will not be.  Example:
 //
-// DEBUG(cerr << "Bitset contains: " << Bitset << "\n");
+// DEBUG(std::cerr &lt;&lt; "Bitset contains: " &lt;&lt; Bitset &lt;&lt; "\n");
 //</i>
-<span class="doc_red">#ifdef NDEBUG
+<span class="doc_hilite">#ifdef NDEBUG
 #define DEBUG(X)
 #else
-#define DEBUG(X)</span> \
-  do { if (DebugFlag) { X; } } while (0)
-<span class="doc_red">#endif</span>
+#define DEBUG(X)</span> do { if (DebugFlag) { X; } } while (0)
+<span class="doc_hilite">#endif</span>
 </pre>
+</div>
 
 <p>This allows clients to blissfully use the <tt>DEBUG()</tt> macro, or the
 <tt>DebugFlag</tt> explicitly if they want to.  Now we just need to be able to
@@ -930,18 +994,19 @@ an additial argument to our command line argument processor, and we specify
 where to fill in with the <a href="#cl::location">cl::location</a>
 attribute:</p>
 
+<div class="doc_code">
 <pre>
-bool DebugFlag;      <i>// the actual value</i>
+bool DebugFlag;                  <i>// the actual value</i>
 static <a href="#cl::opt">cl::opt</a>&lt;bool, true&gt;       <i>// The parser</i>
-Debug("<i>debug</i>", <a href="#cl::desc">cl::desc</a>("<i>Enable debug output</i>")</a>, <a href="#cl::Hidden">cl::Hidden</a>,
-      <a href="#cl::location">cl::location</a>(DebugFlag));
+Debug("<i>debug</i>", <a href="#cl::desc">cl::desc</a>("<i>Enable debug output</i>"), <a href="#cl::Hidden">cl::Hidden</a>, <a href="#cl::location">cl::location</a>(DebugFlag));
 </pre>
+</div>
 
 <p>In the above example, we specify "<tt>true</tt>" as the second argument to
-the <a href="#cl::opt">cl::opt</a> template, indicating that the template should
-not maintain a copy of the value itself.  In addition to this, we specify the <a
-href="#cl::location">cl::location</a> attribute, so that <tt>DebugFlag</tt> is
-automatically set.</p>
+the <tt><a href="#cl::opt">cl::opt</a></tt> template, indicating that the
+template should not maintain a copy of the value itself.  In addition to this,
+we specify the <tt><a href="#cl::location">cl::location</a></tt> attribute, so
+that <tt>DebugFlag</tt> is automatically set.</p>
 
 </div>
 
@@ -992,12 +1057,13 @@ the section on <a href="#storage">Internal vs External Storage</a> for more
 information.</li>
 
 <li><a name="cl::aliasopt">The <b><tt>cl::aliasopt</tt></b></a> attribute
-specifies which option a <a href="#cl::alias">cl::alias</a> option is an alias
-for.</li>
+specifies which option a <tt><a href="#cl::alias">cl::alias</a></tt> option is
+an alias for.</li>
 
 <li><a name="cl::values">The <b><tt>cl::values</tt></b></a> attribute specifies
 the string-to-value mapping to be used by the generic parser.  It takes a
-<b>null terminated</b> list of (option, value, description) triplets that
+<b>clEnumValEnd terminated</b> list of (option, value, description) triplets 
+that
 specify the option name, the value mapped to, and the description shown in the
 <tt>--help</tt> for the tool.  Because the generic parser is used most
 frequently with enum values, two macros are often useful:
@@ -1212,13 +1278,15 @@ Arguments</a> section for more information.</li>
 specifies that this option is used to capture "interpreter style" arguments.  See <a href="#cl::ConsumeAfter">this section for more information</a>.</li>
 
 <li><a name="cl::Prefix">The <b><tt>cl::Prefix</tt></b></a> modifier specifies
-that this option prefixes its value.  With 'Prefix' options, there is no equal
-sign that separates the value from the option name specified.  This is useful
-for processing odd arguments like '<tt>-lmalloc -L/usr/lib'</tt> in a linker
-tool.  Here, the '<tt>l</tt>' and '<tt>L</tt>' options are normal string (list)
-options, that have the <a href="#cl::Prefix">cl::Prefix</a> modifier added to
-allow the CommandLine library to recognize them.  Note that <a
-href="#cl::Prefix">cl::Prefix</a> options must not have the <a
+that this option prefixes its value.  With 'Prefix' options, the equal sign does
+not separate the value from the option name specified. Instead, the value is
+everything after the prefix, including any equal sign if present. This is useful
+for processing odd arguments like <tt>-lmalloc</tt> and <tt>-L/usr/lib</tt> in a
+linker tool or <tt>-DNAME=value</tt> in a compiler tool.   Here, the 
+'<tt>l</tt>', '<tt>D</tt>' and '<tt>L</tt>' options are normal string (or list)
+options, that have the <a href="#cl::Prefix">cl::Prefix</a> modifier added to 
+allow the CommandLine library to recognize them.  Note that 
+<a href="#cl::Prefix">cl::Prefix</a> options must not have the <a
 href="#cl::ValueDisallowed">cl::ValueDisallowed</a> modifier specified.</li>
 
 <li><a name="cl::Grouping">The <b><tt>cl::Grouping</tt></b></a> modifier is used
@@ -1241,6 +1309,7 @@ input option into (potentially multiple) prefix and grouping options.  The
 strategy basically looks like this:</p>
 
 <p><tt>parse(string OrigInput) {</tt>
+
 <ol>
 <li><tt>string input = OrigInput;</tt>
 <li><tt>if (isOption(input)) return getOption(input).parse();</tt>&nbsp;&nbsp;&nbsp;&nbsp;<i>// Normal option</i>
@@ -1254,11 +1323,11 @@ strategy basically looks like this:</p>
 &nbsp;&nbsp;input = OrigInput;<br>
 &nbsp;&nbsp;while (!isOption(input) &amp;&amp; !input.empty()) input.pop_back();<br>
 }</tt>
-<li><tt>if (!OrigInput.empty()) error();</tt>
-</tt>
+<li><tt>if (!OrigInput.empty()) error();</tt></li>
 
 </ol>
-<tt>}</tt></p>
+
+<p><tt>}</tt></p>
 
 </div>
 
@@ -1283,10 +1352,19 @@ options are equivalent when <tt>cl::CommaSeparated</tt> is specified:
 makes sense to be used in a case where the option is allowed to accept one or
 more values (i.e. it is a <a href="#cl::list">cl::list</a> option).</li>
 
+<li><a name="cl::PositionalEatsArgs">The
+<b><tt>cl::PositionalEatsArgs</tt></b></a> modifier (which only applies to
+positional arguments, and only makes sense for lists) indicates that positional
+argument should consume any strings after it (including strings that start with
+a "-") up until another recognized positional argument.  For example, if you
+have two "eating" positional arguments "<tt>pos1</tt>" and "<tt>pos2</tt>" the
+string "<tt>-pos1 -foo -bar baz -pos2 -bork</tt>" would cause the "<tt>-foo -bar
+-baz</tt>" strings to be applied to the "<tt>-pos1</tt>" option and the
+"<tt>-bork</tt>" string to be applied to the "<tt>-pos2</tt>" option.</li>
+
 </ul>
 
-<p>So far, the only miscellaneous option modifier is the
-<tt>cl::CommaSeparated</tt> modifier.</p>
+<p>So far, these are the only two miscellaneous option modifiers.</p>
 
 </div>
 
@@ -1447,6 +1525,34 @@ the conversion from string to data.</p>
 
 </div>
 
+<!-- _______________________________________________________________________ -->
+<div class="doc_subsubsection">
+  <a name="cl::extrahelp">The <tt>cl::extrahelp</tt> class</a>
+</div>
+
+<div class="doc_text">
+
+<p>The <tt>cl::extrahelp</tt> class is a nontemplated class that allows extra
+help text to be printed out for the <tt>--help</tt> option.</p>
+
+<pre>
+<b>namespace</b> cl {
+  <b>struct</b> extrahelp;
+}
+</pre>
+
+<p>To use the extrahelp, simply construct one with a <tt>const char*</tt> 
+parameter to the constructor. The text passed to the constructor will be printed
+at the bottom of the help message, verbatim. Note that multiple
+<tt>cl::extrahelp</tt> <b>can</b> be used but this practice is discouraged. If
+your tool needs to print additional help information, put all that help into a
+single <tt>cl::extrahelp</tt> instance.</p>
+<p>For example:</p>
+<pre>
+  cl::extrahelp("\nADDITIONAL HELP:\n\n  This is the extra help\n");
+</pre>
+</div>
+
 <!-- ======================================================================= -->
 <div class="doc_subsection">
   <a name="builtinparsers">Builtin parsers</a>
@@ -1669,8 +1775,16 @@ tutorial.</p>
 </div>
 
 <div class="doc_text">
+  <p>Several of the LLVM libraries define static <tt>cl::opt</tt> instances that
+  will automatically be included in any program that links with that library.
+  This is a feature. However, sometimes it is necessary to know the value of the
+  command line option outside of the library. In these cases the library does or
+  should provide an external storage location that is accessible to users of the
+  library. Examples of this include the <tt>llvm::DebugFlag</tt> exported by the
+  <tt>lib/Support/Debug.cpp</tt> file and the <tt>llvm::TimePassesIsEnabled</tt>
+  flag exported by the <tt>lib/VMCore/Pass.cpp</tt> file.</p>
 
-<p>TODO: fill in this section</p>
+<p>TODO: complete this section</p>
 
 </div>
 
@@ -1688,12 +1802,16 @@ tutorial.</p>
 <!-- *********************************************************************** -->
 
 <hr>
-<div class="doc_footer">
-  <address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
-  <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a>
-  <br>
+<address>
+  <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
+  src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
+  <a href="http://validator.w3.org/check/referer"><img
+  src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
+
+  <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
+  <a href="http://llvm.cs.uiuc.edu">LLVM Compiler Infrastructure</a><br>
   Last modified: $Date$
-</div>
+</address>
 
 </body>
 </html>