-
Notifications
You must be signed in to change notification settings - Fork 3
/
CommandLineOptions.cpp
149 lines (133 loc) · 5.34 KB
/
CommandLineOptions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Copyright (c) 2019-present, Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CommandLineOptions.h"
#include "Config.h"
#include "Strings.h"
#include <iostream>
#include <string>
#include <type_traits>
#include <llvm/ADT/StringSwitch.h>
void HandleUnknownOption(std::string opt, std::string val, CommandLineOptions &opts)
{
// We format this manually to look like the Logging system because Logging system isn't initialized yet.
std::cout << "[Warning] Ignoring unrecognized parameter \"" << opt << "\" with value " << val << "\n";
}
void SetLoggingAreaOption(std::string opt, std::string val, CommandLineOptions &opts)
{
auto area = llvm::StringSwitch<LoggingArea>(val)
.Case( "ast", LoggingArea::AST)
.Case( "sil", LoggingArea::SIL)
.Case( "ir", LoggingArea:: IR)
.Case( "jit", LoggingArea::JIT)
.Case("importer", LoggingArea::Importer)
.Case( "all", LoggingArea::All)
.Default(LoggingArea::Unknown);
if(area == LoggingArea::Unknown)
std::cout << "[Warning] Ignoring unrecognized logging area \"" << val << "\n";
else
opts.logging_opts.log_areas |= area;
}
void SetLoggingPriorityOption(std::string opt, std::string val, CommandLineOptions &opts)
{
auto priority = llvm::StringSwitch<LoggingPriority>(val)
.Case("info", LoggingPriority::Info)
.Case("warning", LoggingPriority::Warning)
.Case("error", LoggingPriority::Error)
.Case("none", LoggingPriority::None)
.Default(LoggingPriority::Unknown);
if(priority == LoggingPriority::Unknown)
std::cout << "[Warning] Ignoring unrecognized logging priority \"" << val << "\n";
else
opts.logging_opts.min_priority = priority;
}
void SetPlaygroundOption(std::string opt, std::string val, CommandLineOptions &opts)
{
int is_playground = llvm::StringSwitch<int>(val)
.Case("true", 1)
.Case("false", 0)
.Default(-1);
if(is_playground == -1)
std::cout << "[Warning] is_playground is neither \"true\" nor \"false\". Defaulting to \"false\"\n";
opts.is_playground = is_playground == 1;
}
void SetModuleCachePathOption(std::string opt, std::string val, CommandLineOptions &opts)
{
opts.default_module_cache_path = val;
}
void SetPrintToConsoleOption(std::string opt, std::string val, CommandLineOptions &opts)
{
int print_to_console = llvm::StringSwitch<int>(val)
.Case("true", 1)
.Case("false", 0)
.Default(-1);
if(print_to_console == -1)
std::cout << "[Warning] print_to_console is neither \"true\" nor \"false\". Defaulting to \"true\"\n";
opts.print_to_console = print_to_console != 0;
}
void HandleOptionWithoutEquals(std::string arg, CommandLineOptions &opts)
{
// NOTE(sasha): The 2 comes from the length of "-i" or "-l"
if(StartsWith(arg, "-I"))
opts.include_paths.push_back(arg.substr(2));
else if(StartsWith(arg, "-L"))
opts.link_paths.push_back(arg.substr(2));
else if(StartsWith(arg, "-F"))
opts.framework_paths.push_back(arg.substr(2));
else if(arg == "--print_to_console")
opts.print_to_console = true;
else
std::cout << "[Warning] Ignoring unrecognized parameter \"" << arg << "\"\n";
}
void ParseSingleCommandLineOption(std::string arg, CommandLineOptions &opts)
{
using OptionParseFn = std::add_pointer<void(std::string, std::string, CommandLineOptions &)>::type;
auto delimeter_pos = arg.find("=");
if(delimeter_pos == std::string::npos)
return HandleOptionWithoutEquals(arg, opts);
ToLowerCase(arg);
std::string opt = arg.substr(0, delimeter_pos);
std::string val = arg.substr(delimeter_pos + 1, arg.size() - opt.size() - 1);
llvm::StringSwitch<OptionParseFn>(opt)
.Case("--logging", SetLoggingAreaOption)
.Case("--logging_priority", SetLoggingPriorityOption)
.Case("--playground", SetPlaygroundOption)
.Case("--module_cache_path", SetModuleCachePathOption)
.Case("--print_to_console", SetPrintToConsoleOption)
.Default(HandleUnknownOption)
(opt, val, opts);
}
CommandLineOptions DefaultCommandLineOptions()
{
CommandLineOptions opts;
opts.logging_opts.log_areas = LoggingArea::All;
opts.logging_opts.min_priority = LoggingPriority::None;
opts.default_module_cache_path = DEFAULT_MODULE_CACHE_PATH;
opts.is_playground = false;
opts.print_to_console = false;
return opts;
}
//TODO(sasha): Make this more robust to things like -I <path> (with a space)
CommandLineOptions ParseCommandLineOptions(int argc, char **argv)
{
CommandLineOptions result = DefaultCommandLineOptions();
for(int i = 1; i < argc; i++)
{
std::string sanitized_option = argv[i];
Trim(sanitized_option);
ParseSingleCommandLineOption(sanitized_option, result);
}
return result;
}