mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-13 20:32:21 +00:00
Convert from using C style char*'s to strings.
Look ma, no strdups git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
29f921ca75
commit
953e0d7076
@ -17,20 +17,14 @@
|
||||
#ifndef LLVM_SUPPORT_PROGRAMOPTION_H
|
||||
#define LLVM_SUPPORT_PROGRAMOPTION_H
|
||||
|
||||
//************************** System Include Files **************************/
|
||||
|
||||
#include "llvm/Support/Unique.h"
|
||||
#include <string>
|
||||
|
||||
//*************************** User Include Files ***************************/
|
||||
|
||||
#include "llvm/Support/Unique.h"
|
||||
|
||||
//********************** Local Variable Definitions ************************/
|
||||
|
||||
class ProgramOption: public Unique {
|
||||
public:
|
||||
/*ctor*/ ProgramOption (const char* _argString,
|
||||
const char* _helpMesg,
|
||||
/*ctor*/ ProgramOption (const string &_argString,
|
||||
const string &_helpMesg,
|
||||
int _minExpectedArgs = 1)
|
||||
: optionSpecified(false),
|
||||
argString(_argString),
|
||||
@ -46,17 +40,17 @@ public:
|
||||
// were consumed by EvalOpt and should be discarded.
|
||||
// A return value of -1 indicates an error.
|
||||
//
|
||||
virtual int EvalOpt (const char* optarg) = 0;
|
||||
virtual int EvalOpt (const string &) = 0;
|
||||
|
||||
// Returns the value associated with the option as a human-readable
|
||||
// string. The memory returned is allocated via `malloc'.
|
||||
virtual char* GetTextValue () const = 0;
|
||||
// string.
|
||||
virtual string GetTextValue () const = 0;
|
||||
|
||||
// Inline accessor functions for common option information
|
||||
//
|
||||
bool OptionSpecified () const { return optionSpecified; }
|
||||
const char* ArgString () const { return argString.c_str(); }
|
||||
const char* HelpMesg () const { return helpMesg.c_str(); }
|
||||
const string ArgString () const { return argString; }
|
||||
const string HelpMesg () const { return helpMesg; }
|
||||
int MinExpectedArgs () const { return minExpectedArgs; }
|
||||
|
||||
protected:
|
||||
@ -70,19 +64,19 @@ protected:
|
||||
|
||||
class StringOption : public ProgramOption {
|
||||
public:
|
||||
/*ctor*/ StringOption (const char* argString,
|
||||
const char* helpMesg,
|
||||
const char* initValue = "",
|
||||
/*ctor*/ StringOption (const string &argString,
|
||||
const string &helpMesg,
|
||||
const string &initValue = "",
|
||||
bool append = false);
|
||||
// append = false: EvalOpt will overwrite preexisting value
|
||||
// append = true : EvalOpt will append <optArg> to value
|
||||
|
||||
/*dtor*/ virtual ~StringOption () {}
|
||||
|
||||
virtual int EvalOpt (const char* optarg);
|
||||
virtual int EvalOpt (const string &optarg);
|
||||
|
||||
const char* Value () const { return value.c_str(); }
|
||||
virtual char* GetTextValue () const { return strdup(Value()); }
|
||||
const string &Value() const { return value; }
|
||||
virtual string GetTextValue() const { return value; }
|
||||
|
||||
protected:
|
||||
string value;
|
||||
@ -99,50 +93,48 @@ protected:
|
||||
|
||||
class FlagOption : public ProgramOption {
|
||||
public:
|
||||
/*ctor*/ FlagOption (const char* argString,
|
||||
const char* helpMesg,
|
||||
bool initValue = false);
|
||||
FlagOption(const string &argString, const string &helpMesg,
|
||||
bool initValue = false);
|
||||
|
||||
/*dtor*/ virtual ~FlagOption () {}
|
||||
virtual ~FlagOption() {}
|
||||
|
||||
virtual int EvalOpt (const char* optarg);
|
||||
virtual int EvalOpt (const string &optarg);
|
||||
|
||||
bool Value () const { return value; }
|
||||
virtual char* GetTextValue () const { return strdup(
|
||||
value ? "true" : "false");}
|
||||
virtual string GetTextValue() const { return value ? "true" : "false";}
|
||||
private:
|
||||
bool value;
|
||||
bool value;
|
||||
};
|
||||
|
||||
//**************************************************************************/
|
||||
|
||||
class RealValuedOption : public ProgramOption {
|
||||
public:
|
||||
/*ctor*/ RealValuedOption(const char* argString,
|
||||
const char* helpMesg,
|
||||
/*ctor*/ RealValuedOption(const string &argString,
|
||||
const string &helpMesg,
|
||||
double initValue = 0.0);
|
||||
/*dtor*/ virtual ~RealValuedOption() {}
|
||||
|
||||
virtual int EvalOpt (const char* optarg);
|
||||
virtual int EvalOpt (const string &optarg);
|
||||
|
||||
double Value () const { return value; }
|
||||
virtual char* GetTextValue () const;
|
||||
virtual string GetTextValue() const;
|
||||
|
||||
private:
|
||||
double value;
|
||||
double value;
|
||||
};
|
||||
|
||||
//**************************************************************************/
|
||||
|
||||
class IntegerValuedOption : public RealValuedOption {
|
||||
public:
|
||||
/*ctor*/ IntegerValuedOption(const char* argString,
|
||||
const char* helpMesg,
|
||||
/*ctor*/ IntegerValuedOption(const string &argString,
|
||||
const string &helpMesg,
|
||||
int initValue = 0);
|
||||
/*ctor*/ virtual ~IntegerValuedOption() {}
|
||||
|
||||
int Value () const;
|
||||
virtual char* GetTextValue () const;
|
||||
int Value() const;
|
||||
virtual string GetTextValue() const;
|
||||
};
|
||||
|
||||
//**************************************************************************/
|
||||
|
@ -58,12 +58,12 @@ public:
|
||||
// The required argument is specified by the optionString.
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
const char* StringOptionValue(const char* optionString) const;
|
||||
bool FlagOptionValue (const char* optionString) const;
|
||||
double RealOptionValue (const char* optionString) const;
|
||||
int IntOptionValue (const char* optionString) const;
|
||||
string StringOptionValue(const string &optionString) const;
|
||||
bool FlagOptionValue (const string &optionString) const;
|
||||
double RealOptionValue (const string &optionString) const;
|
||||
int IntOptionValue (const string &optionString) const;
|
||||
|
||||
bool OptionSpecified (const char* optionString) const;
|
||||
bool OptionSpecified (const string &optionString) const;
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// The name used to invoke this program.
|
||||
|
@ -14,36 +14,23 @@
|
||||
//
|
||||
//**************************************************************************/
|
||||
|
||||
//************************** System Include Files **************************/
|
||||
|
||||
#include "llvm/Support/ProgramOption.h"
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
|
||||
//*************************** User Include Files ***************************/
|
||||
|
||||
#include "llvm/Support/ProgramOption.h"
|
||||
|
||||
//********************** Local Variable Definitions ************************/
|
||||
|
||||
//************************ Class Implementations ***************************/
|
||||
|
||||
|
||||
//**************************************************************************/
|
||||
|
||||
StringOption::StringOption(const char* _argString,
|
||||
const char* _helpMesg,
|
||||
const char* _initValue,
|
||||
StringOption::StringOption(const string &_argString,
|
||||
const string &_helpMesg,
|
||||
const string &_initValue,
|
||||
bool _append)
|
||||
: ProgramOption(_argString, _helpMesg),
|
||||
value(_initValue),
|
||||
append(_append)
|
||||
{}
|
||||
|
||||
int
|
||||
StringOption::EvalOpt(const char* optarg)
|
||||
{
|
||||
int StringOption::EvalOpt(const string &optarg) {
|
||||
if (optarg == (char*) NULL)
|
||||
return -1; // flag the error
|
||||
|
||||
@ -59,17 +46,15 @@ StringOption::EvalOpt(const char* optarg)
|
||||
|
||||
//**************************************************************************/
|
||||
|
||||
FlagOption::FlagOption(const char* _argString,
|
||||
const char* _helpMesg,
|
||||
FlagOption::FlagOption(const string &_argString,
|
||||
const string &_helpMesg,
|
||||
bool _initValue)
|
||||
: ProgramOption(_argString, _helpMesg, 0),
|
||||
value(_initValue)
|
||||
{}
|
||||
|
||||
int
|
||||
FlagOption::EvalOpt(const char* optarg)
|
||||
{
|
||||
if (strcmp(optarg, "0") == 0) {
|
||||
int FlagOption::EvalOpt(const string &optarg) {
|
||||
if (optarg == "0") {
|
||||
value = false;
|
||||
return 1; // one additional argument consumed
|
||||
}
|
||||
@ -81,21 +66,18 @@ FlagOption::EvalOpt(const char* optarg)
|
||||
|
||||
//**************************************************************************/
|
||||
|
||||
RealValuedOption::RealValuedOption(const char* _argString,
|
||||
const char* _helpMesg,
|
||||
RealValuedOption::RealValuedOption(const string &_argString,
|
||||
const string &_helpMesg,
|
||||
double _initValue)
|
||||
: ProgramOption(_argString, _helpMesg),
|
||||
value(_initValue)
|
||||
{}
|
||||
|
||||
int
|
||||
RealValuedOption::EvalOpt(const char* optarg)
|
||||
{
|
||||
if (optarg == (char*) NULL)
|
||||
return -1;
|
||||
int RealValuedOption::EvalOpt(const string &optarg) {
|
||||
if (optarg == "") return -1;
|
||||
|
||||
char* lastCharScanned = NULL;
|
||||
value = strtod(optarg, &lastCharScanned);
|
||||
value = strtod(optarg.c_str(), &lastCharScanned);
|
||||
if (! (*lastCharScanned == '\0')) // look for incorrect or partially
|
||||
return -1; // correct numerical argument
|
||||
|
||||
@ -103,36 +85,30 @@ RealValuedOption::EvalOpt(const char* optarg)
|
||||
return 1;
|
||||
}
|
||||
|
||||
char*
|
||||
RealValuedOption::GetTextValue() const
|
||||
{
|
||||
string RealValuedOption::GetTextValue() const {
|
||||
char buffer[40];
|
||||
sprintf(buffer, "%f", value);
|
||||
return strdup(buffer);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
//**************************************************************************/
|
||||
|
||||
IntegerValuedOption::IntegerValuedOption(const char* _argString,
|
||||
const char* _helpMesg,
|
||||
IntegerValuedOption::IntegerValuedOption(const string &_argString,
|
||||
const string &_helpMesg,
|
||||
int _initValue)
|
||||
: RealValuedOption(_argString, _helpMesg, (double) _initValue)
|
||||
{}
|
||||
|
||||
int
|
||||
IntegerValuedOption::Value() const
|
||||
{
|
||||
int IntegerValuedOption::Value() const {
|
||||
double realValue = RealValuedOption::Value();
|
||||
assert(realValue == (int) realValue);
|
||||
return (int) realValue;
|
||||
}
|
||||
|
||||
char*
|
||||
IntegerValuedOption::GetTextValue() const
|
||||
{
|
||||
string IntegerValuedOption::GetTextValue() const {
|
||||
char buffer[40];
|
||||
sprintf(buffer, "%d", Value());
|
||||
return strdup(buffer);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
//**************************************************************************/
|
||||
|
||||
|
@ -19,7 +19,6 @@
|
||||
|
||||
#include <iostream.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
@ -45,22 +44,20 @@ ProgramOptions::ProgramOptions(int _argc,
|
||||
argsConsumed(0)
|
||||
{}
|
||||
|
||||
const char*
|
||||
ProgramOptions::StringOptionValue(const char* optString) const
|
||||
{
|
||||
const StringOption* handler = (const StringOption*) OptionHandler(optString);
|
||||
return (handler == NULL) ? NULL : handler->Value();
|
||||
string ProgramOptions::StringOptionValue(const string &optString) const {
|
||||
const StringOption* handler = (const StringOption*)OptionHandler(optString);
|
||||
return (handler == NULL) ? string("") : handler->Value();
|
||||
}
|
||||
|
||||
bool
|
||||
ProgramOptions::FlagOptionValue(const char* optString) const
|
||||
ProgramOptions::FlagOptionValue(const string &optString) const
|
||||
{
|
||||
const FlagOption* handler = (const FlagOption*) OptionHandler(optString);
|
||||
return (handler == NULL) ? false : handler->Value();
|
||||
}
|
||||
|
||||
double
|
||||
ProgramOptions::RealOptionValue(const char* optString) const
|
||||
ProgramOptions::RealOptionValue(const string &optString) const
|
||||
{
|
||||
const RealValuedOption* handler =
|
||||
(const RealValuedOption*) OptionHandler(optString);
|
||||
@ -68,7 +65,7 @@ ProgramOptions::RealOptionValue(const char* optString) const
|
||||
}
|
||||
|
||||
int
|
||||
ProgramOptions::IntOptionValue(const char* optString) const
|
||||
ProgramOptions::IntOptionValue(const string &optString) const
|
||||
{
|
||||
const IntegerValuedOption* handler =
|
||||
(const IntegerValuedOption*) OptionHandler(optString);
|
||||
@ -76,7 +73,7 @@ ProgramOptions::IntOptionValue(const char* optString) const
|
||||
}
|
||||
|
||||
bool
|
||||
ProgramOptions::OptionSpecified(const char* optString) const
|
||||
ProgramOptions::OptionSpecified(const string &optString) const
|
||||
{
|
||||
const ProgramOption* handler = OptionHandler(optString);
|
||||
return handler->OptionSpecified();
|
||||
@ -225,8 +222,8 @@ ProgramOptions::PrintOptions(ostream& stream) const
|
||||
for (i=1; i <= handler->MinExpectedArgs(); i++)
|
||||
stream << showarg;
|
||||
|
||||
int numCharsPrinted = 1 + strlen(handler->ArgString())
|
||||
+ strlen(showarg) * handler->MinExpectedArgs();
|
||||
int numCharsPrinted = 1 + handler->ArgString().length()
|
||||
+ 6 * handler->MinExpectedArgs();
|
||||
for (i=1; i > numCharsPrinted / 8; i--)
|
||||
stream << "\t";
|
||||
|
||||
|
@ -83,8 +83,8 @@ LLCOptions::ParseExtraArgs()
|
||||
|
||||
// output file name may be specified with -o option;
|
||||
// otherwise create it from the input file name by replace ".ll" with ".o"
|
||||
const char* outfilenameOpt = this->StringOptionValue(OUTFILENAME_OPT);
|
||||
if (outfilenameOpt)
|
||||
const string &outfilenameOpt = StringOptionValue(OUTFILENAME_OPT);
|
||||
if (outfilenameOpt.length())
|
||||
{// "-o" option was used
|
||||
outputFileName = outfilenameOpt;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user