This is an archive of the discontinued LLVM Phabricator instance.

Driver: support detecting driver mode when clang has a version suffix without dash (PR21094)
ClosedPublic

Authored by hans on Oct 16 2014, 1:46 PM.

Details

Summary

Clang would previously not get into C++ mode when invoked as 'clang++3.6' (though clang++-3.6 would work).

I found the previous loop logic in this function confusing; hopefully this makes it clearer.

Diff Detail

Repository
rL LLVM

Event Timeline

hans updated this revision to Diff 15045.Oct 16 2014, 1:46 PM
hans retitled this revision from to Driver: support detecting driver mode when clang has a version suffix without dash (PR21094).
hans updated this object.
hans edited the test plan for this revision. (Show Details)
hans added reviewers: dblaikie, majnemer.
hans added subscribers: Unknown Object (MLST), hansw.
dblaikie edited edge metadata.Oct 16 2014, 2:02 PM

Just some thoughts - mostly quite optional.

test/Driver/parse-progname.c
1 ↗(On Diff #15045)

Yay tests!

Is "REQUIRES: shell" sufficient to get ln? I would wonder if we'd get a shell on Windows (mingw, perhaps) where symlinks weren't possible - but I guess maybe mingw fakes it somehow.

tools/driver/driver.cpp
209 ↗(On Diff #15045)

possible cleanup: could remove the duplicated "--driver-mode=" from these strings & just build it into the use of these strings instead.

222 ↗(On Diff #15045)

The array itself could be moved inside this function if it returned a const DriverSuffix*, which seems like a nice API for it. (null if not found, presumably)

223 ↗(On Diff #15045)

I still rather like my std::find_if (& it'd be simpler once the struct had a name), but I get that it's not everyone's cup of tea.

239 ↗(On Diff #15045)

Prefer copy/move initialization over direct initialization (makes it clear to the reader that no explicit conversions are happening here)?

245 ↗(On Diff #15045)

Prefer copy/move initialization over direct initialization (makes it clear to the reader that no explicit conversions are happening here)?

249 ↗(On Diff #15045)

this seems like a bit of a special case (& rather repetitious) - could we not just have a single call to FindDriverSuffix search for an arbitrary substring rather than a suffix?

What if someone has clang++-my-branch ? (perhaps not very common, I suppose - but still, a substring search seems simpler and more accepting - would it go wrong somewhere?)

284 ↗(On Diff #15045)

Hardcoding the size of the array (while obvious in the previous line) isn't ideal - the begin/end formulation I committed or the prior array_lengthof use, seem preferable.

hans added a comment.Oct 16 2014, 2:49 PM

Thanks for the review!

test/Driver/parse-progname.c
1 ↗(On Diff #15045)

I hope it's enough. I was inspired by some other test that required shell and seems to successfully copy stuff around and use sed.

In an msys shell, there is ln for example, though I don't know if it is faking it or uses some kind of magic.

tools/driver/driver.cpp
209 ↗(On Diff #15045)

I'd rather skip on this, since it will require allocating storage for the string concatenation somewhere, so making this code simpler will make the code that adds the flag to the arguments less straight-forward.

222 ↗(On Diff #15045)

Yes, that's much nicer. Done.

223 ↗(On Diff #15045)

I think my loop is easier to read, but I'm pretty old-fashioned :)

239 ↗(On Diff #15045)

Done.

245 ↗(On Diff #15045)

Done.

249 ↗(On Diff #15045)

I'm hesitant to change the existing suffix-matching logic too much. clang++-my-branch won't work, but it didn't work before my patch either.

Also it wouldn't be completely straight-forward: cl is a substring of clang, so we'd have to be somewhat smart, and with a target prefix mixed in... hmm.

284 ↗(On Diff #15045)

Yes, the std::begin and end were much nicer. Done.

hans updated this revision to Diff 15051.Oct 16 2014, 2:50 PM
hans edited edge metadata.

Addressing comments.

dblaikie added inline comments.Oct 16 2014, 3:59 PM
tools/driver/driver.cpp
209 ↗(On Diff #15045)

Oh, fair enough - that makes sense. Thanks for the explanation.

249 ↗(On Diff #15045)

OK then - but at this point I'm a bit too confused by the logic here (test if it's a suffix, if it isn't, strip off any digits and dots (a version suffix), try to find the suffix again, then strip off any -foo component and try again...

Oh, I see one problem then, maybe... (next comment)

250 ↗(On Diff #15045)

Hmm, pity this is essentially std::string's find_last_not_of, except that you can't readily pass the set of all digits and '.'. I could imagine some other STL algorithms that I might find more readable, but probably only about as much as the std::find_if discussed previously.

251 ↗(On Diff #15045)

the extra parens around the isDigit call are a bit confusing/misleading (I saw the end ')' and thought it was maybe grouped with the empty() call above for some reason)

254 ↗(On Diff #15045)

Should this line be inside the prior if (but after/outside the loop)?

hans added inline comments.Oct 16 2014, 5:16 PM
tools/driver/driver.cpp
251 ↗(On Diff #15045)

The extra parenthesis are around the || expression.

Anyway, I've rewritten this with less parenthesis, and fitting on one line.

254 ↗(On Diff #15045)

D'oh, yes it should. Done.

hans updated this revision to Diff 15056.Oct 16 2014, 5:16 PM

Address comments.

dblaikie accepted this revision.Oct 16 2014, 7:05 PM
dblaikie edited edge metadata.

Looks good

tools/driver/driver.cpp
251 ↗(On Diff #15045)

Oh, right - I just can't count. Sorry about that.

The new version isn't necessarily 'portable' to interesting digit characters in other languages? Maybe? Dunno if that's important.

If you're going to write it that way, I'll at least optionally suggest that you could use StringRef's find_last_not_of to do that search.

This revision is now accepted and ready to land.Oct 16 2014, 7:05 PM
dblaikie added inline comments.Oct 16 2014, 7:10 PM
tools/driver/driver.cpp
258 ↗(On Diff #15056)

Might help if these comments were maybe illustrated with an example:

// Strip trailing version: clang-3.4 -> clang

// Strip trailing -component: clang-foo -> clang
hans added inline comments.Oct 17 2014, 10:17 AM
tools/driver/driver.cpp
258 ↗(On Diff #15056)

Done.

251 ↗(On Diff #15045)

I found that StringRef has an rtrim(); I think that's what we want here.

hans closed this revision.Oct 17 2014, 10:18 AM
hans updated this revision to Diff 15084.

Closed by commit rL220052 (authored by @hans).