Add a getopt implementation for Windows. For now this just supports shortopts. Reviewed by csilvers.
See http://code.google.com/p/include-what-you-use/issues/detail?id=20.
This commit is contained in:
parent
37816ef3e8
commit
c38d8de4e0
|
@ -25,6 +25,7 @@ add_clang_executable(include-what-you-use
|
|||
iwyu_ast_util.cc
|
||||
iwyu_cache.cc
|
||||
iwyu_driver.cc
|
||||
iwyu_getopt.cc
|
||||
iwyu_globals.cc
|
||||
iwyu_include_picker.cc
|
||||
iwyu_lexer_utils.cc
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
//===--- iwyu_getopt.cc - OS specific implementation of getopt for iwyu ---===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "iwyu_getopt.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
#include <string.h>
|
||||
|
||||
// This code is derived from http://www.codeproject.com/KB/cpp/xgetopt.aspx
|
||||
// Originally written by Hans Dietrich, and released into the public domain.
|
||||
|
||||
char *optarg = NULL;
|
||||
int optind = 0;
|
||||
|
||||
int getopt_long(int argc, char *const *argv, const char *shortopts,
|
||||
const struct option *longopts, int *longind)
|
||||
{
|
||||
static char * next = NULL;
|
||||
if(optind == 0)
|
||||
next = NULL;
|
||||
optarg = NULL;
|
||||
if (next == NULL || *next == 0)
|
||||
{
|
||||
if (optind == 0)
|
||||
optind++;
|
||||
|
||||
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == 0)
|
||||
{
|
||||
optarg = NULL;
|
||||
if (optind < argc)
|
||||
optarg = argv[optind];
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strcmp(argv[optind], "--") == 0)
|
||||
{
|
||||
optind++;
|
||||
optarg = NULL;
|
||||
if (optind < argc)
|
||||
optarg = argv[optind];
|
||||
return -1;
|
||||
}
|
||||
|
||||
next = argv[optind];
|
||||
next++; // skip past -
|
||||
optind++;
|
||||
}
|
||||
|
||||
char c = *next++;
|
||||
const char *cp = strchr(shortopts, c);
|
||||
|
||||
if (cp == NULL || c == ':')
|
||||
return '?';
|
||||
|
||||
cp++;
|
||||
if (*cp == ':')
|
||||
{
|
||||
if (*next != 0)
|
||||
{
|
||||
optarg = next;
|
||||
next = NULL;
|
||||
}
|
||||
else if (optind < argc)
|
||||
{
|
||||
optarg = argv[optind];
|
||||
optind++;
|
||||
}
|
||||
else
|
||||
{
|
||||
return '?';
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
#endif // #if defined(_MSC_VER)
|
|
@ -0,0 +1,43 @@
|
|||
//===--- iwyu_getopt.h - OS specific implementation of getopt for iwyu ----===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_GETOPT_H_
|
||||
#define DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_GETOPT_H_
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
// We provide a partial implementation of getopt_long for Windows.
|
||||
// For now the longopts struct is ignored, and only shortopts are parsed.
|
||||
// This would all normally be defined in getopt.h.
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
|
||||
#define no_argument 0
|
||||
#define required_argument 1
|
||||
#define optional_argument 2
|
||||
|
||||
struct option
|
||||
{
|
||||
const char *name;
|
||||
int has_arg;
|
||||
int *flag;
|
||||
int val;
|
||||
};
|
||||
|
||||
extern int getopt_long(int argc, char *const *argv, const char *shortopts,
|
||||
const struct option *longopts, int *longind);
|
||||
|
||||
|
||||
#else // #if defined(_MSC_VER)
|
||||
|
||||
#include <getopt.h> // IWYU pragma: export
|
||||
|
||||
#endif // #if defined(_MSC_VER)
|
||||
|
||||
#endif // DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_GETOPT_H_
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
#include "iwyu_globals.h"
|
||||
|
||||
#include <getopt.h> // for optarg, required_argument, etc
|
||||
#include <stdio.h> // for printf
|
||||
#include <stdlib.h> // for atoi, exit, getenv
|
||||
#include <algorithm> // for sort, make_pair
|
||||
|
@ -19,6 +18,7 @@
|
|||
#include <utility> // for make_pair, pair
|
||||
|
||||
#include "iwyu_cache.h"
|
||||
#include "iwyu_getopt.h"
|
||||
#include "iwyu_include_picker.h"
|
||||
#include "iwyu_lexer_utils.h"
|
||||
#include "iwyu_location_util.h"
|
||||
|
|
Loading…
Reference in New Issue