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