comparison unittests/Support/CommandLineTest.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children 3a76565eade5
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 #include "llvm/Support/CommandLine.h"
11 #include "llvm/ADT/STLExtras.h"
10 #include "llvm/ADT/SmallString.h" 12 #include "llvm/ADT/SmallString.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Config/config.h" 13 #include "llvm/Config/config.h"
13 #include "llvm/Support/CommandLine.h"
14 #include "llvm/Support/FileSystem.h" 14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Path.h" 15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/Program.h"
16 #include "llvm/Support/StringSaver.h" 17 #include "llvm/Support/StringSaver.h"
17 #include "gtest/gtest.h" 18 #include "gtest/gtest.h"
18 #include <fstream> 19 #include <fstream>
19 #include <stdlib.h> 20 #include <stdlib.h>
20 #include <string> 21 #include <string>
178 BumpPtrAllocator A; 179 BumpPtrAllocator A;
179 StringSaver Saver(A); 180 StringSaver Saver(A);
180 parse(Input, Saver, Actual, /*MarkEOLs=*/false); 181 parse(Input, Saver, Actual, /*MarkEOLs=*/false);
181 EXPECT_EQ(OutputSize, Actual.size()); 182 EXPECT_EQ(OutputSize, Actual.size());
182 for (unsigned I = 0, E = Actual.size(); I != E; ++I) { 183 for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
183 if (I < OutputSize) 184 if (I < OutputSize) {
184 EXPECT_STREQ(Output[I], Actual[I]); 185 EXPECT_STREQ(Output[I], Actual[I]);
186 }
185 } 187 }
186 } 188 }
187 189
188 TEST(CommandLineTest, TokenizeGNUCommandLine) { 190 TEST(CommandLineTest, TokenizeGNUCommandLine) {
189 const char Input[] = 191 const char Input[] =
301 303
302 EXPECT_FALSE(TopLevelOpt); 304 EXPECT_FALSE(TopLevelOpt);
303 EXPECT_FALSE(SC1Opt); 305 EXPECT_FALSE(SC1Opt);
304 EXPECT_FALSE(SC2Opt); 306 EXPECT_FALSE(SC2Opt);
305 const char *args[] = {"prog", "-top-level"}; 307 const char *args[] = {"prog", "-top-level"};
306 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true)); 308 EXPECT_TRUE(
309 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
307 EXPECT_TRUE(TopLevelOpt); 310 EXPECT_TRUE(TopLevelOpt);
308 EXPECT_FALSE(SC1Opt); 311 EXPECT_FALSE(SC1Opt);
309 EXPECT_FALSE(SC2Opt); 312 EXPECT_FALSE(SC2Opt);
310 313
311 TopLevelOpt = false; 314 TopLevelOpt = false;
313 cl::ResetAllOptionOccurrences(); 316 cl::ResetAllOptionOccurrences();
314 EXPECT_FALSE(TopLevelOpt); 317 EXPECT_FALSE(TopLevelOpt);
315 EXPECT_FALSE(SC1Opt); 318 EXPECT_FALSE(SC1Opt);
316 EXPECT_FALSE(SC2Opt); 319 EXPECT_FALSE(SC2Opt);
317 const char *args2[] = {"prog", "sc1", "-sc1"}; 320 const char *args2[] = {"prog", "sc1", "-sc1"};
318 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true)); 321 EXPECT_TRUE(
322 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
319 EXPECT_FALSE(TopLevelOpt); 323 EXPECT_FALSE(TopLevelOpt);
320 EXPECT_TRUE(SC1Opt); 324 EXPECT_TRUE(SC1Opt);
321 EXPECT_FALSE(SC2Opt); 325 EXPECT_FALSE(SC2Opt);
322 326
323 SC1Opt = false; 327 SC1Opt = false;
325 cl::ResetAllOptionOccurrences(); 329 cl::ResetAllOptionOccurrences();
326 EXPECT_FALSE(TopLevelOpt); 330 EXPECT_FALSE(TopLevelOpt);
327 EXPECT_FALSE(SC1Opt); 331 EXPECT_FALSE(SC1Opt);
328 EXPECT_FALSE(SC2Opt); 332 EXPECT_FALSE(SC2Opt);
329 const char *args3[] = {"prog", "sc2", "-sc2"}; 333 const char *args3[] = {"prog", "sc2", "-sc2"};
330 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), true)); 334 EXPECT_TRUE(
335 cl::ParseCommandLineOptions(3, args3, StringRef(), &llvm::nulls()));
331 EXPECT_FALSE(TopLevelOpt); 336 EXPECT_FALSE(TopLevelOpt);
332 EXPECT_FALSE(SC1Opt); 337 EXPECT_FALSE(SC1Opt);
333 EXPECT_TRUE(SC2Opt); 338 EXPECT_TRUE(SC2Opt);
334 } 339 }
335 340
340 StackSubCommand SC2("sc2", "Second subcommand"); 345 StackSubCommand SC2("sc2", "Second subcommand");
341 346
342 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false)); 347 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false));
343 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false)); 348 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
344 349
350 std::string Errs;
351 raw_string_ostream OS(Errs);
352
345 const char *args[] = {"prog", "sc1", "-sc2"}; 353 const char *args[] = {"prog", "sc1", "-sc2"};
346 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), true)); 354 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
355 OS.flush();
356 EXPECT_FALSE(Errs.empty());
347 } 357 }
348 358
349 TEST(CommandLineTest, AddToAllSubCommands) { 359 TEST(CommandLineTest, AddToAllSubCommands) {
350 cl::ResetCommandLineParser(); 360 cl::ResetCommandLineParser();
351 361
356 366
357 const char *args[] = {"prog", "-everywhere"}; 367 const char *args[] = {"prog", "-everywhere"};
358 const char *args2[] = {"prog", "sc1", "-everywhere"}; 368 const char *args2[] = {"prog", "sc1", "-everywhere"};
359 const char *args3[] = {"prog", "sc2", "-everywhere"}; 369 const char *args3[] = {"prog", "sc2", "-everywhere"};
360 370
371 std::string Errs;
372 raw_string_ostream OS(Errs);
373
361 EXPECT_FALSE(AllOpt); 374 EXPECT_FALSE(AllOpt);
362 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true)); 375 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), &OS));
363 EXPECT_TRUE(AllOpt); 376 EXPECT_TRUE(AllOpt);
364 377
365 AllOpt = false; 378 AllOpt = false;
366 379
367 cl::ResetAllOptionOccurrences(); 380 cl::ResetAllOptionOccurrences();
368 EXPECT_FALSE(AllOpt); 381 EXPECT_FALSE(AllOpt);
369 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true)); 382 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), &OS));
370 EXPECT_TRUE(AllOpt); 383 EXPECT_TRUE(AllOpt);
371 384
372 AllOpt = false; 385 AllOpt = false;
373 386
374 cl::ResetAllOptionOccurrences(); 387 cl::ResetAllOptionOccurrences();
375 EXPECT_FALSE(AllOpt); 388 EXPECT_FALSE(AllOpt);
376 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), true)); 389 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), &OS));
377 EXPECT_TRUE(AllOpt); 390 EXPECT_TRUE(AllOpt);
391
392 // Since all parsing succeeded, the error message should be empty.
393 OS.flush();
394 EXPECT_TRUE(Errs.empty());
378 } 395 }
379 396
380 TEST(CommandLineTest, ReparseCommandLineOptions) { 397 TEST(CommandLineTest, ReparseCommandLineOptions) {
381 cl::ResetCommandLineParser(); 398 cl::ResetCommandLineParser();
382 399
384 cl::init(false)); 401 cl::init(false));
385 402
386 const char *args[] = {"prog", "-top-level"}; 403 const char *args[] = {"prog", "-top-level"};
387 404
388 EXPECT_FALSE(TopLevelOpt); 405 EXPECT_FALSE(TopLevelOpt);
389 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true)); 406 EXPECT_TRUE(
407 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
390 EXPECT_TRUE(TopLevelOpt); 408 EXPECT_TRUE(TopLevelOpt);
391 409
392 TopLevelOpt = false; 410 TopLevelOpt = false;
393 411
394 cl::ResetAllOptionOccurrences(); 412 cl::ResetAllOptionOccurrences();
395 EXPECT_FALSE(TopLevelOpt); 413 EXPECT_FALSE(TopLevelOpt);
396 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true)); 414 EXPECT_TRUE(
415 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
397 EXPECT_TRUE(TopLevelOpt); 416 EXPECT_TRUE(TopLevelOpt);
398 } 417 }
399 418
400 TEST(CommandLineTest, RemoveFromRegularSubCommand) { 419 TEST(CommandLineTest, RemoveFromRegularSubCommand) {
401 cl::ResetCommandLineParser(); 420 cl::ResetCommandLineParser();
404 StackOption<bool> RemoveOption("remove-option", cl::sub(SC), cl::init(false)); 423 StackOption<bool> RemoveOption("remove-option", cl::sub(SC), cl::init(false));
405 StackOption<bool> KeepOption("keep-option", cl::sub(SC), cl::init(false)); 424 StackOption<bool> KeepOption("keep-option", cl::sub(SC), cl::init(false));
406 425
407 const char *args[] = {"prog", "sc", "-remove-option"}; 426 const char *args[] = {"prog", "sc", "-remove-option"};
408 427
428 std::string Errs;
429 raw_string_ostream OS(Errs);
430
409 EXPECT_FALSE(RemoveOption); 431 EXPECT_FALSE(RemoveOption);
410 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, StringRef(), true)); 432 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
411 EXPECT_TRUE(RemoveOption); 433 EXPECT_TRUE(RemoveOption);
434 OS.flush();
435 EXPECT_TRUE(Errs.empty());
412 436
413 RemoveOption.removeArgument(); 437 RemoveOption.removeArgument();
414 438
415 cl::ResetAllOptionOccurrences(); 439 cl::ResetAllOptionOccurrences();
416 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), true)); 440 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
441 OS.flush();
442 EXPECT_FALSE(Errs.empty());
417 } 443 }
418 444
419 TEST(CommandLineTest, RemoveFromTopLevelSubCommand) { 445 TEST(CommandLineTest, RemoveFromTopLevelSubCommand) {
420 cl::ResetCommandLineParser(); 446 cl::ResetCommandLineParser();
421 447
425 "top-level-keep", cl::sub(*cl::TopLevelSubCommand), cl::init(false)); 451 "top-level-keep", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
426 452
427 const char *args[] = {"prog", "-top-level-remove"}; 453 const char *args[] = {"prog", "-top-level-remove"};
428 454
429 EXPECT_FALSE(TopLevelRemove); 455 EXPECT_FALSE(TopLevelRemove);
430 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true)); 456 EXPECT_TRUE(
457 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
431 EXPECT_TRUE(TopLevelRemove); 458 EXPECT_TRUE(TopLevelRemove);
432 459
433 TopLevelRemove.removeArgument(); 460 TopLevelRemove.removeArgument();
434 461
435 cl::ResetAllOptionOccurrences(); 462 cl::ResetAllOptionOccurrences();
436 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args, StringRef(), true)); 463 EXPECT_FALSE(
464 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
437 } 465 }
438 466
439 TEST(CommandLineTest, RemoveFromAllSubCommands) { 467 TEST(CommandLineTest, RemoveFromAllSubCommands) {
440 cl::ResetCommandLineParser(); 468 cl::ResetCommandLineParser();
441 469
450 const char *args1[] = {"prog", "sc1", "-remove-option"}; 478 const char *args1[] = {"prog", "sc1", "-remove-option"};
451 const char *args2[] = {"prog", "sc2", "-remove-option"}; 479 const char *args2[] = {"prog", "sc2", "-remove-option"};
452 480
453 // It should work for all subcommands including the top-level. 481 // It should work for all subcommands including the top-level.
454 EXPECT_FALSE(RemoveOption); 482 EXPECT_FALSE(RemoveOption);
455 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, StringRef(), true)); 483 EXPECT_TRUE(
484 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
456 EXPECT_TRUE(RemoveOption); 485 EXPECT_TRUE(RemoveOption);
457 486
458 RemoveOption = false; 487 RemoveOption = false;
459 488
460 cl::ResetAllOptionOccurrences(); 489 cl::ResetAllOptionOccurrences();
461 EXPECT_FALSE(RemoveOption); 490 EXPECT_FALSE(RemoveOption);
462 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args1, StringRef(), true)); 491 EXPECT_TRUE(
492 cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
463 EXPECT_TRUE(RemoveOption); 493 EXPECT_TRUE(RemoveOption);
464 494
465 RemoveOption = false; 495 RemoveOption = false;
466 496
467 cl::ResetAllOptionOccurrences(); 497 cl::ResetAllOptionOccurrences();
468 EXPECT_FALSE(RemoveOption); 498 EXPECT_FALSE(RemoveOption);
469 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true)); 499 EXPECT_TRUE(
500 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
470 EXPECT_TRUE(RemoveOption); 501 EXPECT_TRUE(RemoveOption);
471 502
472 RemoveOption.removeArgument(); 503 RemoveOption.removeArgument();
473 504
474 // It should not work for any subcommands including the top-level. 505 // It should not work for any subcommands including the top-level.
475 cl::ResetAllOptionOccurrences(); 506 cl::ResetAllOptionOccurrences();
476 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args0, StringRef(), true)); 507 EXPECT_FALSE(
477 cl::ResetAllOptionOccurrences(); 508 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
478 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args1, StringRef(), true)); 509 cl::ResetAllOptionOccurrences();
479 cl::ResetAllOptionOccurrences(); 510 EXPECT_FALSE(
480 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, StringRef(), true)); 511 cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
512 cl::ResetAllOptionOccurrences();
513 EXPECT_FALSE(
514 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
481 } 515 }
482 516
483 TEST(CommandLineTest, GetRegisteredSubcommands) { 517 TEST(CommandLineTest, GetRegisteredSubcommands) {
484 cl::ResetCommandLineParser(); 518 cl::ResetCommandLineParser();
485 519
489 StackOption<bool> Opt2("opt2", cl::sub(SC2), cl::init(false)); 523 StackOption<bool> Opt2("opt2", cl::sub(SC2), cl::init(false));
490 524
491 const char *args0[] = {"prog", "sc1"}; 525 const char *args0[] = {"prog", "sc1"};
492 const char *args1[] = {"prog", "sc2"}; 526 const char *args1[] = {"prog", "sc2"};
493 527
494 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, StringRef(), true)); 528 EXPECT_TRUE(
529 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
495 EXPECT_FALSE(Opt1); 530 EXPECT_FALSE(Opt1);
496 EXPECT_FALSE(Opt2); 531 EXPECT_FALSE(Opt2);
497 for (auto *S : cl::getRegisteredSubcommands()) { 532 for (auto *S : cl::getRegisteredSubcommands()) {
498 if (*S) 533 if (*S) {
499 EXPECT_EQ("sc1", S->getName()); 534 EXPECT_EQ("sc1", S->getName());
500 } 535 }
501 536 }
502 cl::ResetAllOptionOccurrences(); 537
503 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args1, StringRef(), true)); 538 cl::ResetAllOptionOccurrences();
539 EXPECT_TRUE(
540 cl::ParseCommandLineOptions(2, args1, StringRef(), &llvm::nulls()));
504 EXPECT_FALSE(Opt1); 541 EXPECT_FALSE(Opt1);
505 EXPECT_FALSE(Opt2); 542 EXPECT_FALSE(Opt2);
506 for (auto *S : cl::getRegisteredSubcommands()) { 543 for (auto *S : cl::getRegisteredSubcommands()) {
507 if (*S) 544 if (*S) {
508 EXPECT_EQ("sc2", S->getName()); 545 EXPECT_EQ("sc2", S->getName());
509 } 546 }
547 }
548 }
549
550 TEST(CommandLineTest, ArgumentLimit) {
551 std::string args(32 * 4096, 'a');
552 EXPECT_FALSE(llvm::sys::commandLineFitsWithinSystemLimits("cl", args.data()));
510 } 553 }
511 554
512 TEST(CommandLineTest, ResponseFiles) { 555 TEST(CommandLineTest, ResponseFiles) {
513 llvm::SmallString<128> TestDir; 556 llvm::SmallString<128> TestDir;
514 std::error_code EC = 557 std::error_code EC =
568 llvm::sys::fs::remove(IncDir); 611 llvm::sys::fs::remove(IncDir);
569 llvm::sys::fs::remove(IncludedFileName); 612 llvm::sys::fs::remove(IncludedFileName);
570 llvm::sys::fs::remove(TestDir); 613 llvm::sys::fs::remove(TestDir);
571 } 614 }
572 615
616 TEST(CommandLineTest, SetDefautValue) {
617 cl::ResetCommandLineParser();
618
619 StackOption<std::string> Opt1("opt1", cl::init("true"));
620 StackOption<bool> Opt2("opt2", cl::init(true));
621 cl::alias Alias("alias", llvm::cl::aliasopt(Opt2));
622 StackOption<int> Opt3("opt3", cl::init(3));
623
624 const char *args[] = {"prog", "-opt1=false", "-opt2", "-opt3"};
625
626 EXPECT_TRUE(
627 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
628
629 EXPECT_TRUE(Opt1 == "false");
630 EXPECT_TRUE(Opt2);
631 EXPECT_TRUE(Opt3 == 3);
632
633 Opt2 = false;
634 Opt3 = 1;
635
636 cl::ResetAllOptionOccurrences();
637
638 for (auto &OM : cl::getRegisteredOptions(*cl::TopLevelSubCommand)) {
639 cl::Option *O = OM.second;
640 if (O->ArgStr == "opt2") {
641 continue;
642 }
643 O->setDefault();
644 }
645
646 EXPECT_TRUE(Opt1 == "true");
647 EXPECT_TRUE(Opt2);
648 EXPECT_TRUE(Opt3 == 3);
649 }
650
573 } // anonymous namespace 651 } // anonymous namespace