• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "options.h"
18 
19 #include <android-base/logging.h>
20 #include <android-base/parseint.h>
21 #include <android-base/result.h>
22 #include <android-base/strings.h>
23 #include <getopt.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 
27 #include <algorithm>
28 #include <iostream>
29 #include <sstream>
30 #include <string>
31 
32 #include "aidl_language.h"
33 #include "logging.h"
34 #include "os.h"
35 
36 using android::base::Result;
37 using android::base::Split;
38 using android::base::Trim;
39 using std::endl;
40 using std::string;
41 
42 #ifndef PLATFORM_SDK_VERSION
43 #define PLATFORM_SDK_VERSION "<UNKNOWN>"
44 #endif
45 
46 namespace android {
47 namespace aidl {
48 
GetUsage() const49 string Options::GetUsage() const {
50   std::ostringstream sstr;
51   sstr << "AIDL Compiler: built for platform SDK version " << PLATFORM_SDK_VERSION << endl;
52   sstr << "usage:" << endl
53        << myname_ << " --lang={java|cpp|ndk|rust} [OPTION]... INPUT..." << endl
54        << "   Generate Java, C++ or Rust files for AIDL file(s)." << endl
55        << endl
56        << myname_ << " --preprocess OUTPUT INPUT..." << endl
57        << "   Create an AIDL file having declarations of AIDL file(s)." << endl
58        << endl
59        << myname_ << " --dumpapi --out=DIR INPUT..." << endl
60        << "   Dump API signature of AIDL file(s) to DIR." << endl
61        << endl
62        << myname_ << " --checkapi[={compatible|equal}] OLD_DIR NEW_DIR" << endl
63        << "   Check whether NEW_DIR API dump is {compatible|equal} extension " << endl
64        << "   of the API dump OLD_DIR. Default: compatible" << endl
65        << endl
66        << myname_ << " --apimapping OUTPUT INPUT..." << endl
67        << "   Generate a mapping of declared aidl method signatures to" << endl
68        << "   the original line number. e.g.: " << endl
69        << "       If line 39 of foo/bar/IFoo.aidl contains:"
70        << "         void doFoo(int bar, String baz);" << endl
71        << "       Then the result would be:" << endl
72        << "         foo.bar.Baz|doFoo|int,String,|void" << endl
73        << "         foo/bar/IFoo.aidl:39" << endl
74        << endl;
75 
76   // Legacy option formats
77   if (language_ == Options::Language::JAVA) {
78     sstr << myname_ << " [OPTION]... INPUT [OUTPUT]" << endl
79          << "   Generate a Java file for an AIDL file." << endl
80          << endl;
81   } else if (language_ == Options::Language::CPP) {
82     sstr << myname_ << " [OPTION]... INPUT HEADER_DIR OUTPUT" << endl
83          << "   Generate C++ headers and source for an AIDL file." << endl
84          << endl;
85   } else if (language_ == Options::Language::RUST) {
86     sstr << myname_ << " [OPTION]... INPUT [OUTPUT]" << endl
87          << "   Generate Rust file for an AIDL file." << endl
88          << endl;
89   }
90 
91   sstr << "OPTION:" << endl
92        << "  -I DIR, --include=DIR" << endl
93        << "          Use DIR as a search path for import statements." << endl
94        << "  -p FILE, --preprocessed=FILE" << endl
95        << "          Include FILE which is created by --preprocess." << endl
96        << "  -d FILE, --dep=FILE" << endl
97        << "          Generate dependency file as FILE. Don't use this when" << endl
98        << "          there are multiple input files. Use -a then." << endl
99        << "  -o DIR, --out=DIR" << endl
100        << "          Use DIR as the base output directory for generated files." << endl
101        << "  -h DIR, --header_out=DIR" << endl
102        << "          Generate C++ headers under DIR." << endl
103        << "  -a" << endl
104        << "          Generate dependency file next to the output file with the" << endl
105        << "          name based on the input file." << endl
106        << "  -b" << endl
107        << "          Trigger fail when trying to compile a parcelable declaration." << endl
108        << "  --ninja" << endl
109        << "          Generate dependency file in a format ninja understands." << endl
110        << "  --rpc" << endl
111        << "          (for Java) whether to generate support for RPC transactions." << endl
112        << "  --structured" << endl
113        << "          Whether this interface is defined exclusively in AIDL." << endl
114        << "          It is therefore a candidate for stabilization." << endl
115        << "  --stability=<level>" << endl
116        << "          The stability requirement of this interface." << endl
117        << "  --min_sdk_version=<version>" << endl
118        << "          Minimum SDK version that the generated code should support." << endl
119        << "          Defaults to " << DEFAULT_SDK_VERSION_JAVA << " for --lang=java, " << endl
120        << "            " << DEFAULT_SDK_VERSION_CPP << " for --lang=cpp, " << endl
121        << "            " << DEFAULT_SDK_VERSION_NDK << " for --lang=ndk, " << endl
122        << "            " << DEFAULT_SDK_VERSION_RUST << " for --lang=rust, " << endl
123        << "  -t, --trace" << endl
124        << "          Include tracing code for systrace. Note that if either" << endl
125        << "          the client or service code is not auto-generated by this" << endl
126        << "          tool, that part will not be traced." << endl
127        << "  --transaction_names" << endl
128        << "          Generate transaction names." << endl
129        << "  -v VER, --version=VER" << endl
130        << "          Set the version of the interface and parcelable to VER." << endl
131        << "          VER must be an interger greater than 0." << endl
132        << "  --hash=HASH" << endl
133        << "          Set the interface hash to HASH." << endl
134        << "  --log" << endl
135        << "          Information about the transaction, e.g., method name, argument" << endl
136        << "          values, execution time, etc., is provided via callback." << endl
137        << "  -Werror" << endl
138        << "          Turn warnings into errors." << endl
139        << "  -Wno-error=<warning>" << endl
140        << "          Turn the specified warning into a warning even if -Werror is specified."
141        << endl
142        << "  -W<warning>" << endl
143        << "          Enable the specified warning." << endl
144        << "  -Wno-<warning>" << endl
145        << "          Disable the specified warning." << endl
146        << "  -w" << endl
147        << "          Disable all diagnostics. -w wins -Weverything" << endl
148        << "  -Weverything" << endl
149        << "          Enable all diagnostics." << endl
150        << "  --help" << endl
151        << "          Show this help." << endl
152        << endl
153        << "INPUT:" << endl
154        << "  An AIDL file." << endl
155        << endl
156        << "OUTPUT:" << endl
157        << "  Path to the generated Java or C++ source file. This is ignored when" << endl
158        << "  -o or --out is specified or the number of the input files are" << endl
159        << "  more than one." << endl
160        << "  For Java, if omitted, Java source file is generated at the same" << endl
161        << "  place as the input AIDL file," << endl
162        << endl
163        << "HEADER_DIR:" << endl
164        << "  Path to where C++ headers are generated." << endl;
165   return sstr.str();
166 }
167 
to_string(Options::Language language)168 string to_string(Options::Language language) {
169   switch (language) {
170     case Options::Language::CPP:
171       return "cpp";
172     case Options::Language::JAVA:
173       return "java";
174     case Options::Language::NDK:
175       return "ndk";
176     case Options::Language::RUST:
177       return "rust";
178     case Options::Language::CPP_ANALYZER:
179       return "cpp-analyzer";
180     case Options::Language::UNSPECIFIED:
181       return "unspecified";
182     default:
183       AIDL_FATAL(AIDL_LOCATION_HERE)
184           << "Unexpected Options::Language enumerator: " << static_cast<size_t>(language);
185   }
186 }
187 
StabilityFromString(const std::string & stability,Stability * out_stability)188 bool Options::StabilityFromString(const std::string& stability, Stability* out_stability) {
189   if (stability == "vintf") {
190     *out_stability = Stability::VINTF;
191     return true;
192   }
193   return false;
194 }
195 
196 static const std::map<std::string, uint32_t> codeNameToVersion = {
197     {"S", 31},
198     {"Tiramisu", SDK_VERSION_Tiramisu},
199     {"UpsideDownCake", SDK_VERSION_UpsideDownCake},
200     // this is an alias for the latest in-development platform version
201     {"current", SDK_VERSION_current},
202     // this is an alias for use of all APIs, including those not in any API surface
203     {"platform_apis", 10001},
204 };
205 
MinSdkVersionFromString(const std::string & str)206 Result<uint32_t> MinSdkVersionFromString(const std::string& str) {
207   uint32_t num;
208   if (!android::base::ParseUint(str, &num, 10000u /* max */)) {
209     if (auto found = codeNameToVersion.find(str); found != codeNameToVersion.end()) {
210       return found->second;
211     }
212     return Errorf("Invalid SDK version: {}", str);
213   }
214   return num;
215 }
216 
DefaultMinSdkVersionForLang(const Options::Language lang)217 static uint32_t DefaultMinSdkVersionForLang(const Options::Language lang) {
218   switch (lang) {
219     case Options::Language::CPP:
220       return DEFAULT_SDK_VERSION_CPP;
221     case Options::Language::JAVA:
222       return DEFAULT_SDK_VERSION_JAVA;
223     case Options::Language::NDK:
224       return DEFAULT_SDK_VERSION_NDK;
225     case Options::Language::RUST:
226       return DEFAULT_SDK_VERSION_RUST;
227     case Options::Language::CPP_ANALYZER:
228       return DEFAULT_SDK_VERSION_CPP;
229     case Options::Language::UNSPECIFIED:
230       return DEFAULT_SDK_VERSION_JAVA;  // The safest option
231     default:
232       AIDL_FATAL(AIDL_LOCATION_HERE)
233           << "Unexpected Options::Language enumerator: " << static_cast<size_t>(lang);
234   }
235 }
236 
From(const string & cmdline)237 Options Options::From(const string& cmdline) {
238   vector<string> args = Split(cmdline, " ");
239   return From(args);
240 }
241 
From(const vector<string> & args)242 Options Options::From(const vector<string>& args) {
243   Options::Language lang = Options::Language::JAVA;
244   int argc = args.size();
245   if (argc >= 1 && args.at(0) == "aidl-cpp") {
246     lang = Options::Language::CPP;
247   }
248   const char* argv[argc + 1];
249   for (int i = 0; i < argc; i++) {
250     argv[i] = args.at(i).c_str();
251   }
252   argv[argc] = nullptr;
253 
254   return Options(argc, argv, lang);
255 }
256 
Options(int argc,const char * const raw_argv[],Options::Language default_lang)257 Options::Options(int argc, const char* const raw_argv[], Options::Language default_lang)
258     : myname_(argc >= 1 ? raw_argv[0] : "aidl"), language_(default_lang) {
259   std::vector<const char*> argv = warning_options_.Parse(argc, raw_argv, error_message_);
260   if (!Ok()) return;
261   argc = argv.size();
262 
263   bool lang_option_found = false;
264   optind = 0;
265   while (true) {
266     static struct option long_options[] = {
267         {"lang", required_argument, 0, 'l'},
268         {"preprocess", no_argument, 0, 's'},
269         {"dumpapi", no_argument, 0, 'u'},
270         {"no_license", no_argument, 0, 'x'},
271         {"checkapi", optional_argument, 0, 'A'},
272         {"apimapping", required_argument, 0, 'i'},
273         {"include", required_argument, 0, 'I'},
274         {"preprocessed", required_argument, 0, 'p'},
275         {"dep", required_argument, 0, 'd'},
276         {"out", required_argument, 0, 'o'},
277         {"header_out", required_argument, 0, 'h'},
278         {"ninja", no_argument, 0, 'n'},
279         {"rpc", no_argument, 0, 'r'},
280         {"stability", required_argument, 0, 'Y'},
281         {"min_sdk_version", required_argument, 0, 'm'},
282         {"structured", no_argument, 0, 'S'},
283         {"trace", no_argument, 0, 't'},
284         {"transaction_names", no_argument, 0, 'c'},
285         {"version", required_argument, 0, 'v'},
286         {"log", no_argument, 0, 'L'},
287         {"hash", required_argument, 0, 'H'},
288         {"help", no_argument, 0, 'e'},
289         {0, 0, 0, 0},
290     };
291     const int c = getopt_long(argc, const_cast<char* const*>(argv.data()),
292                               "I:p:d:o:h:abtv:i:", long_options, nullptr);
293     if (c == -1) {
294       // no more options
295       break;
296     }
297     switch (c) {
298       case 'l':
299         if (language_ == Options::Language::CPP) {
300           // aidl-cpp can't set language. aidl-cpp exists only for backwards
301           // compatibility.
302           error_message_ << "aidl-cpp does not support --lang." << endl;
303           return;
304         } else {
305           lang_option_found = true;
306           string lang = Trim(optarg);
307           if (lang == "java") {
308             language_ = Options::Language::JAVA;
309             task_ = Options::Task::COMPILE;
310           } else if (lang == "cpp") {
311             language_ = Options::Language::CPP;
312             task_ = Options::Task::COMPILE;
313           } else if (lang == "ndk") {
314             language_ = Options::Language::NDK;
315             task_ = Options::Task::COMPILE;
316           } else if (lang == "rust") {
317             language_ = Options::Language::RUST;
318             task_ = Options::Task::COMPILE;
319           } else if (lang == "cpp-analyzer") {
320             language_ = Options::Language::CPP_ANALYZER;
321             task_ = Options::Task::COMPILE;
322           } else {
323             error_message_ << "Unsupported language: '" << lang << "'" << endl;
324             return;
325           }
326         }
327         break;
328       case 's':
329         task_ = Options::Task::PREPROCESS;
330         break;
331       case 'u':
332         task_ = Options::Task::DUMP_API;
333         break;
334       case 'x':
335         dump_no_license_ = true;
336         break;
337       case 'A':
338         task_ = Options::Task::CHECK_API;
339         // to ensure that all parcelables in the api dumpes are structured
340         structured_ = true;
341         if (optarg) {
342           if (strcmp(optarg, "compatible") == 0)
343             check_api_level_ = CheckApiLevel::COMPATIBLE;
344           else if (strcmp(optarg, "equal") == 0)
345             check_api_level_ = CheckApiLevel::EQUAL;
346           else {
347             error_message_ << "Unsupported --checkapi level: '" << optarg << "'" << endl;
348             return;
349           }
350         }
351         break;
352       case 'I': {
353         import_dirs_.emplace(Trim(optarg));
354         break;
355       }
356       case 'p':
357         preprocessed_files_.emplace_back(Trim(optarg));
358         break;
359       case 'd':
360         dependency_file_ = Trim(optarg);
361         break;
362       case 'o':
363         output_dir_ = Trim(optarg);
364         if (output_dir_.back() != OS_PATH_SEPARATOR) {
365           output_dir_.push_back(OS_PATH_SEPARATOR);
366         }
367         break;
368       case 'h':
369         output_header_dir_ = Trim(optarg);
370         if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
371           output_header_dir_.push_back(OS_PATH_SEPARATOR);
372         }
373         break;
374       case 'n':
375         dependency_file_ninja_ = true;
376         break;
377       case 'S':
378         structured_ = true;
379         break;
380       case 'Y': {
381         const string stability_str = Trim(optarg);
382         if (!StabilityFromString(stability_str, &stability_)) {
383           error_message_ << "Unrecognized stability level: '" << stability_str
384                          << "'. Must be vintf." << endl;
385           return;
386         }
387         break;
388       }
389       case 'm':
390         if (auto ret = MinSdkVersionFromString(Trim(optarg)); ret.ok()) {
391           min_sdk_version_ = *ret;
392         } else {
393           error_message_ << ret.error();
394           return;
395         }
396         break;
397       case 'r':
398         gen_rpc_ = true;
399         break;
400       case 't':
401         gen_traces_ = true;
402         break;
403       case 'a':
404         auto_dep_file_ = true;
405         break;
406       case 'b':
407         fail_on_parcelable_ = true;
408         break;
409       case 'c':
410         gen_transaction_names_ = true;
411         break;
412       case 'v': {
413         const string ver_str = Trim(optarg);
414         int ver = atoi(ver_str.c_str());
415         if (ver > 0) {
416           version_ = ver;
417         } else {
418           error_message_ << "Invalid version number: '" << ver_str << "'. "
419                          << "Version must be a positive natural number." << endl;
420           return;
421         }
422         break;
423       }
424       case 'H':
425         hash_ = Trim(optarg);
426         break;
427       case 'L':
428         gen_log_ = true;
429         break;
430       case 'e':
431         std::cerr << GetUsage();
432         task_ = Task::HELP;
433         CHECK(Ok());
434         return;
435       case 'i':
436         output_file_ = Trim(optarg);
437         task_ = Task::DUMP_MAPPINGS;
438         break;
439       default:
440         error_message_ << GetUsage();
441         CHECK(!Ok());
442         return;
443     }
444   }  // while
445 
446   // Positional arguments
447   if (!lang_option_found && task_ == Options::Task::COMPILE) {
448     // the legacy arguments format
449     if (argc - optind <= 0) {
450       error_message_ << "No input file" << endl;
451       return;
452     }
453     if (language_ == Options::Language::JAVA || language_ == Options::Language::RUST) {
454       input_files_.emplace_back(argv[optind++]);
455       if (argc - optind >= 1) {
456         output_file_ = argv[optind++];
457       } else if (output_dir_.empty()) {
458         // when output is omitted and -o option isn't set, the output is by
459         // default set to the input file path with .aidl is replaced to .java.
460         // If -o option is set, the output path is calculated by
461         // GetOutputFilePath which returns "<output_dir>/<package/name>/
462         // <typename>.java"
463         output_file_ = input_files_.front();
464         if (android::base::EndsWith(output_file_, ".aidl")) {
465           output_file_ = output_file_.substr(0, output_file_.length() - strlen(".aidl"));
466         }
467         output_file_ += (language_ == Options::Language::JAVA) ? ".java" : ".rs";
468       }
469     } else if (IsCppOutput()) {
470       input_files_.emplace_back(argv[optind++]);
471       if (argc - optind < 2) {
472         error_message_ << "No HEADER_DIR or OUTPUT." << endl;
473         return;
474       }
475       output_header_dir_ = argv[optind++];
476       if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
477         output_header_dir_.push_back(OS_PATH_SEPARATOR);
478       }
479       output_file_ = argv[optind++];
480     }
481     if (argc - optind > 0) {
482       error_message_ << "Too many arguments: ";
483       for (int i = optind; i < argc; i++) {
484         error_message_ << " " << argv[i];
485       }
486       error_message_ << endl;
487     }
488   } else {
489     // the new arguments format
490     if (task_ == Options::Task::COMPILE || task_ == Options::Task::DUMP_API ||
491         task_ == Options::Task::DUMP_MAPPINGS) {
492       if (argc - optind < 1) {
493         error_message_ << "No input file." << endl;
494         return;
495       }
496     } else {
497       if (argc - optind < 2) {
498         error_message_ << "Insufficient arguments. At least 2 required, but "
499                        << "got " << (argc - optind) << "." << endl;
500         return;
501       }
502       if (task_ != Options::Task::CHECK_API) {
503         output_file_ = argv[optind++];
504       }
505     }
506     while (optind < argc) {
507       input_files_.emplace_back(argv[optind++]);
508     }
509   }
510 
511   // filter out invalid combinations
512   if (lang_option_found) {
513     if (IsCppOutput() && task_ == Options::Task::COMPILE) {
514       if (output_dir_.empty()) {
515         error_message_ << "Output directory is not set. Set with --out." << endl;
516         return;
517       }
518       if (output_header_dir_.empty()) {
519         error_message_ << "Header output directory is not set. Set with "
520                        << "--header_out." << endl;
521         return;
522       }
523     }
524     if (language_ == Options::Language::JAVA && task_ == Options::Task::COMPILE) {
525       if (output_dir_.empty()) {
526         error_message_ << "Output directory is not set. Set with --out." << endl;
527         return;
528       }
529       if (!output_header_dir_.empty()) {
530         error_message_ << "Header output directory is set, which does not make "
531                        << "sense for Java." << endl;
532         return;
533       }
534     }
535     if (language_ == Options::Language::RUST && task_ == Options::Task::COMPILE) {
536       if (output_dir_.empty()) {
537         error_message_ << "Output directory is not set. Set with --out." << endl;
538         return;
539       }
540       if (!output_header_dir_.empty()) {
541         error_message_ << "Header output directory is set, which does not make "
542                        << "sense for Rust." << endl;
543         return;
544       }
545     }
546   }
547   if (task_ == Options::Task::COMPILE) {
548     for (const string& input : input_files_) {
549       if (!android::base::EndsWith(input, ".aidl")) {
550         error_message_ << "Expected .aidl file for input but got '" << input << "'" << endl;
551         return;
552       }
553     }
554     if (!output_file_.empty() && input_files_.size() > 1) {
555       error_message_ << "Multiple AIDL files can't be compiled to a single "
556                      << "output file '" << output_file_ << "'. "
557                      << "Use --out=DIR instead for output files." << endl;
558       return;
559     }
560     if (!dependency_file_.empty() && input_files_.size() > 1) {
561       error_message_ << "-d or --dep doesn't work when compiling multiple AIDL "
562                      << "files. Use '-a' to generate dependency file next to "
563                      << "the output file with the name based on the input "
564                      << "file." << endl;
565       return;
566     }
567     if (gen_log_ && (language_ != Options::Language::CPP && language_ != Options::Language::NDK)) {
568       error_message_ << "--log is currently supported for either --lang=cpp or --lang=ndk" << endl;
569       return;
570     }
571   }
572   if (task_ == Options::Task::PREPROCESS) {
573     if (version_ > 0) {
574       error_message_ << "--version should not be used with '--preprocess'." << endl;
575       return;
576     }
577   }
578   if (task_ == Options::Task::CHECK_API) {
579     if (input_files_.size() != 2) {
580       error_message_ << "--checkapi requires two inputs for comparing, "
581                      << "but got " << input_files_.size() << "." << endl;
582       return;
583     }
584   }
585   if (task_ == Options::Task::DUMP_API) {
586     if (output_dir_.empty()) {
587       error_message_ << "--dumpapi requires output directory. Use --out." << endl;
588       return;
589     }
590   }
591   if (task_ != Options::Task::COMPILE) {
592     if (min_sdk_version_ != 0) {
593       error_message_ << "--min_sdk_version is available only for compilation." << endl;
594       return;
595     }
596     // For other tasks, use "current"
597     min_sdk_version_ = MinSdkVersionFromString("current").value();
598   }
599 
600   uint32_t default_ver = DefaultMinSdkVersionForLang(language_);
601   if (min_sdk_version_ == 0) {  // --min_sdk_version flag not specified
602     min_sdk_version_ = default_ver;
603   } else if (min_sdk_version_ < default_ver) {
604     error_message_ << "Min SDK version should at least be " << default_ver << "." << endl;
605     return;
606   }
607 
608   uint32_t rpc_version = MinSdkVersionFromString("Tiramisu").value();
609   // note: we would like to always generate (Java) code to support RPC out of
610   // the box, but doing so causes an unclear error for people trying to use RPC
611   // - now we require them to add the gen_rpc build rule and get this clear message.
612   if (gen_rpc_ && min_sdk_version_ < rpc_version) {
613     error_message_ << "RPC code requires minimum SDK version of at least " << rpc_version << endl;
614     return;
615   }
616 
617   if (min_sdk_version_ >= rpc_version) gen_rpc_ = true;
618 
619   AIDL_FATAL_IF(!output_dir_.empty() && output_dir_.back() != OS_PATH_SEPARATOR, output_dir_);
620   AIDL_FATAL_IF(!output_header_dir_.empty() && output_header_dir_.back() != OS_PATH_SEPARATOR,
621                 output_header_dir_);
622 }
623 
Parse(int argc,const char * const raw_argv[],ErrorMessage & error_message)624 std::vector<const char*> WarningOptions::Parse(int argc, const char* const raw_argv[],
625                                                ErrorMessage& error_message) {
626   std::vector<const char*> remains;
627   for (int i = 0; i < argc; i++) {
628     auto arg = raw_argv[i];
629     if (strcmp(arg, "-Weverything") == 0) {
630       enable_all_ = true;
631     } else if (strcmp(arg, "-Werror") == 0) {
632       as_errors_ = true;
633     } else if (strcmp(arg, "-w") == 0) {
634       disable_all_ = true;
635     } else if (base::StartsWith(arg, "-Wno-error=")) {
636       no_errors_.insert(arg + strlen("-Wno-error="));
637     } else if (base::StartsWith(arg, "-Wno-")) {
638       disabled_.insert(arg + strlen("-Wno-"));
639     } else if (base::StartsWith(arg, "-W")) {
640       enabled_.insert(arg + strlen("-W"));
641     } else {
642       remains.push_back(arg);
643     }
644   }
645 
646   for (const auto& names : {no_errors_, disabled_, enabled_}) {
647     for (const auto& name : names) {
648       if (kAllDiagnostics.count(name) == 0) {
649         error_message << "unknown warning: " << name << "\n";
650         return {};
651       }
652     }
653   }
654 
655   return remains;
656 }
657 
GetDiagnosticMapping() const658 DiagnosticMapping WarningOptions::GetDiagnosticMapping() const {
659   DiagnosticMapping mapping;
660   for (const auto& [_, d] : kAllDiagnostics) {
661     bool enabled = d.default_enabled;
662     if (enable_all_ || enabled_.find(d.name) != enabled_.end()) {
663       enabled = true;
664     }
665     if (disable_all_ || disabled_.find(d.name) != disabled_.end()) {
666       enabled = false;
667     }
668 
669     DiagnosticSeverity severity = DiagnosticSeverity::DISABLED;
670     if (enabled) {
671       severity = DiagnosticSeverity::WARNING;
672       if (as_errors_ && no_errors_.find(d.name) == no_errors_.end()) {
673         severity = DiagnosticSeverity::ERROR;
674       }
675     }
676     mapping.Severity(d.id, severity);
677   }
678   return mapping;
679 }
680 
681 }  // namespace aidl
682 }  // namespace android
683