This is an archive of the discontinued LLVM Phabricator instance.

[include-mapping] Implement language separation in stdlib recognizer library
ClosedPublic

Authored by VitaNuo on Jan 31 2023, 9:44 AM.

Diff Detail

Event Timeline

VitaNuo created this revision.Jan 31 2023, 9:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 31 2023, 9:44 AM
VitaNuo requested review of this revision.Jan 31 2023, 9:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 31 2023, 9:44 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
VitaNuo updated this revision to Diff 493651.Jan 31 2023, 9:45 AM

Remove comments.

VitaNuo updated this revision to Diff 493863.Feb 1 2023, 1:03 AM

Minor improvements.

hokein added a comment.Feb 1 2023, 1:57 AM

Can you add some tests in StandardLibraryTest.cpp?

clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
30

using a map here seems like an overkill, we have just 2 elements, I'd just use two separate variables (CMapping, and CXXMapping)

123

nit: just for (Lang Language: {Lang::C, Lang::CXX}) or two statements initilize(Lang::C); and initialize(Lang::CXX);.

212

Do we need the ensureInitialized() here? looks like no needed, we have called it in the Recognizer constructor,

233

nit: just use LangOpts.CPlusPlus to check the language.

VitaNuo updated this revision to Diff 493886.Feb 1 2023, 3:04 AM

Address review comments.

Thanks for the comments!

clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
30

what about the design idea that we might potentially want to extend this to multiple standards etc.? The idea is that it's extensible to ObjC, OpenCL... and so on and so forth, as has been discussed offline.

123

yes, same argument as above. I remember extensive discussions about the idea that we might want to extend this to multiple language versions etc. in the future.

212

you're right, not needed.

233

There's LangStandard::isCPlusPlus method that I've just discovered. That's probably even better.

VitaNuo updated this revision to Diff 493918.Feb 1 2023, 5:37 AM

Add a couple more test cases to StandardLibraryTest.cpp.

Added a couple of test cases.

hokein added inline comments.Feb 2 2023, 4:33 AM
clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
30

I think having a generic Lang enum structure is sufficient for the future extension, and I don't think we're going to add other languages in the foreseeable future (that's why I value the simple implementation at the beginning).

But you're right, getting the implementation right is probably a good idea. I'd like to remove the DenseMap, just use a raw array, something like below should work

enum Lang {
   C = 0,
   CXX,

   LastValue = CXX,
};

// access by e.g. LanguageMappings[static_cast<unsigned>(Lang::C)].
static SymbolHeaderMapping* LanguageMappings[static_cast<unsigned>(Lang::LastValue) + 1];
233

sorry if I was not clearer -- there is a bit CPlusPlus in the LangOptions which already does the equivalent thing (see https://github.com/llvm/llvm-project/blob/main/clang/lib/Basic/LangOptions.cpp#L107).

So we can just use if (D->getLangOpts().CPlusPlus).

clang/unittests/Tooling/StandardLibraryTest.cpp
138

nit: just EXPECT_FALSE(stdlib::Symbol::named("", "int16_t"))), following the existing pattern L46.

145

The existing TEST(StdlibTest, All) test seems like a good umbrella for the this test and the above test, how about moving them to the All test?

VitaNuo updated this revision to Diff 494329.Feb 2 2023, 9:01 AM

Address review comments.

VitaNuo updated this revision to Diff 494332.Feb 2 2023, 9:05 AM

Remove extra include.

Thanks for the comments!

clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
30

Ok, this will need some casting, but I don't have a strong opinion.

233

Ah ok, thanks.

clang/unittests/Tooling/StandardLibraryTest.cpp
138

Right, thanks.

145

Ok, I've moved everything to existing test cases.

hokein accepted this revision.Feb 3 2023, 5:40 AM

Thanks, this looks good to me!

This requires some work to rebase for https://github.com/llvm/llvm-project/commit/e1aaa314a46cd303019da117bfd330611d5b7a84, I will rebase it and land it for you.

clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
30

yeah, it is the sad bit, but I think it is OK.

This revision is now accepted and ready to land.Feb 3 2023, 5:40 AM
hokein updated this revision to Diff 494611.Feb 3 2023, 6:33 AM

rebase to main