This is an archive of the discontinued LLVM Phabricator instance.

[clang] Use a StringRef instead of a raw char pointer to store builtin and call information
ClosedPublic

Authored by serge-sans-paille on Dec 12 2022, 1:47 PM.

Diff Detail

Event Timeline

Herald added a reviewer: MaskRay. · View Herald Transcript
Herald added a reviewer: NoQ. · View Herald Transcript
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
Herald added a reviewer: Restricted Project. · View Herald Transcript
serge-sans-paille requested review of this revision.Dec 12 2022, 1:47 PM
Herald added projects: Restricted Project, Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptDec 12 2022, 1:47 PM
kadircet added inline comments.Dec 13 2022, 12:45 AM
clang-tools-extra/clangd/CompileCommands.cpp
469–474

this is actually an incorrect change (even builds shouldn't be succeeding). as the values here can also be nullptr (which cannot be stored in a StringLiteral) but moreover we later on assign these to Prefixes array, which is of type char*, hence the conversion should also be failing.

but in general i'd actually expect people to be assigning "nullptr"s to these char*s, hence if this was a purely mechanical migration without some extra constraints, it might still blow up at runtime even if it succeeds compiles.

clang-tools-extra/clangd/CompileCommands.cpp
469–474

Builds (and test suite) all succeed (at least locally :-)). This patch also modifies tablegen to *not* generate nullptr, but empty string instead. The constructor of OptTable::Info has been modified to turn these "Empty string terminated array" into regular ArrayRef, which makes iteration code much more straight forward.

kadircet added inline comments.Dec 13 2022, 2:57 AM
clang-tools-extra/clangd/CompileCommands.cpp
469–474

This patch also modifies tablegen to *not* generate nullptr

Oh i see, i definitely missed that one. It might be better to somehow separate that piece out (or at least mention somewhere in the patch description).

But nevertheless, as mentioned these values get assigned into Prefixes array mentioned above, which is a char *. hence the conversion must fail. are you sure you have clang-tools-extra in your enabled projects?

this also checks for a nullptr below (which I marked in a separate comment).

506

this place for example is still checking for nullptrs as termination.

514–515

also this place.

The StaticAnalyzer portion looks good to me AFAICT.

clang/lib/StaticAnalyzer/Core/CallDescription.cpp
39

Maybe we could restrict it even more to StringLiteral. The same applies to the other ctor.

Make Prefixes an array of StringRef too

serge-sans-paille marked 4 inline comments as done.Dec 14 2022, 5:53 AM

New version passes ninja check with clang, clang-tools-extra and lld.

clang/lib/StaticAnalyzer/Core/CallDescription.cpp
39

It's not a strong requirements to have a StringLiteral here, as we generate an std::string from it anyway. Plus the conversion from *intiializer list of string literals* to ArrayRef<StringLiteral> is not infered by the compiler, so I'd rather not go that way (although I like the idea).

kadircet added inline comments.Dec 16 2022, 1:45 AM
clang-tools-extra/clangd/CompileCommands.cpp
469–474

this is still wrong semantically (and i believe are failing tests/builds, you can see it in the premerge bot builds for yourself https://buildkite.com/llvm-project/premerge-checks/builds/126513#018510e5-592b-453e-a213-a2cffc9c9ac2).

This was an array of pointers to null-terminated string blocks. Now you're turning it into just an array of strings.

IIUC, your intention is to change a list of strings terminated with a nullptr into an arrayref. so this should itself be an ArrayRef<StringLiteral>s.

470

why not directly store ArrayRef's here? instead of an empty string terminated array? (same for rest of the patch)

506

previously this was checking for an empty array, now it's checking for an empty string. so semantics are completely diffrenet.

515–516

not even sure what was your intention here, but this was previously iterating over all the possible alternatives of a prefix until it hit the nullptr terminator. now you're making it iterate over something completely different (which i don't know how to describe).

Fix and simplify representation.

The trick here is to use std::initializer_list to store array literals and pass them to the OptTable::Info constructor. As it's fine to have an empty initializer_list and there's already a constructor from initializer_liost to ArrayRef, this avoids most of the workarounds of previous patch.

Also fix an implementation bug in clangd in previous implementation.

serge-sans-paille marked 3 inline comments as done.Dec 16 2022, 12:38 PM
nikic accepted this revision.Dec 22 2022, 7:51 AM

LGTM

This revision is now accepted and ready to land.Dec 22 2022, 7:51 AM
vitalybuka reopened this revision.Dec 25 2022, 11:13 PM
This revision is now accepted and ready to land.Dec 25 2022, 11:13 PM