From: Daniel Dunbar Date: Tue, 7 Apr 2009 18:21:47 +0000 (+0000) Subject: Driver: Fix a parsing bug where some options were matched X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4ae24e7f118f500994772cb051be2e3343b68c1d;p=clang Driver: Fix a parsing bug where some options were matched incorrectly. I'm blanking on the smartest way to write this search, but we should just do the right thing when we move to TableGen. - [driver] -Wextra-tokens isn't parsed correctly git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68525 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Driver/OptTable.cpp b/lib/Driver/OptTable.cpp index 15b121c45b..cbbeea1974 100644 --- a/lib/Driver/OptTable.cpp +++ b/lib/Driver/OptTable.cpp @@ -230,19 +230,26 @@ Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const { struct Info *Start = OptionInfos + FirstSearchableOption - 1; struct Info *End = OptionInfos + LastOption - 1; - // Find the first option which could be a prefix. + // Search for the first next option which could be a prefix. Start = std::lower_bound(Start, End, Str); - // Scan for first option which is a proper prefix. - for (; Start != End; ++Start) - if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0) - break; - - // Look for a match until we don't have a prefix. + // Options are stored in sorted order, with '\0' at the end of the + // alphabet. Since the only options which can accept a string must + // prefix it, we iteratively search for the next option which could + // be a prefix. + // + // FIXME: This is searching much more than necessary, but I am + // blanking on the simplest way to make it fast. We can solve this + // problem when we move to TableGen. for (; Start != End; ++Start) { - if (memcmp(Start->Name, Str, strlen(Start->Name)) != 0) + // Scan for first option which is a proper prefix. + for (; Start != End; ++Start) + if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0) + break; + if (Start == End) break; + // See if this option matches. options::ID id = (options::ID) (Start - OptionInfos + 1); if (Arg *A = getOption(id)->accept(Args, Index)) return A; diff --git a/test/Driver/parsing.c b/test/Driver/parsing.c index ed70949a01..7b6444050d 100644 --- a/test/Driver/parsing.c +++ b/test/Driver/parsing.c @@ -15,6 +15,10 @@ // RUN: not clang -sectalign 1 2 2> %t && // RUN: grep "error: argument to '-sectalign' is missing (expected 3 values)" %t && +// Verify that search continues after find the first option. +// RUN: clang -ccc-print-options -Wally 2> %t && +// RUN: grep 'Option 0 - Name: "-W", Values: {"ally"}' %t && + // RUN: true