This is an archive of the discontinued LLVM Phabricator instance.

[clang][Lex] Add back PPCallbacks::FileNotFound
ClosedPublic

Authored by Hahnfeld on Jan 20 2023, 2:19 AM.

Details

Summary

This callback was removed with commit rG7a124f4859, but we use it downstream in ROOT/Cling to implement handling of a special include syntax. Add back a "safe" version of the callback that only takes the file name and return a bool to silently skip the file.

Diff Detail

Event Timeline

Hahnfeld created this revision.Jan 20 2023, 2:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 20 2023, 2:19 AM
Hahnfeld requested review of this revision.Jan 20 2023, 2:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 20 2023, 2:19 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

I just checked and this "simplified" version of the callback would give us enough information to implement all we need in ROOT (plus some, the bool return value is nicer than temporarily playing with SuppressIncludeNotFoundError). For an upstream test, I could implement a unit test in clang/unittests/Lex/PPCallbacksTest.cpp or a "toy" tooling where #include <file.h?> is ignored if file.h doesn't exist? What do you think (assuming the general idea of adding this callback is fine)?

I just checked and this "simplified" version of the callback would give us enough information to implement all we need in ROOT (plus some, the bool return value is nicer than temporarily playing with SuppressIncludeNotFoundError). For an upstream test, I could implement a unit test in clang/unittests/Lex/PPCallbacksTest.cpp or a "toy" tooling where #include <file.h?> is ignored if file.h doesn't exist? What do you think (assuming the general idea of adding this callback is fine)?

Unit test in clang/unittests/Lex/PPCallbacksTest.cpp sounds good, go for it!

Hahnfeld updated this revision to Diff 491095.Jan 21 2023, 1:02 PM

Add unit test.

jansvoboda11 accepted this revision.Jan 23 2023, 10:21 AM

LGTM with improved wording.

clang/include/clang/Lex/PPCallbacks.h
87

The wording is a bit misleading due to:

if (SuppressIncludeNotFoundError)
  return std::nullopt;

after invoking this callback. Reading the comment, I assume the diagnostic has already been issued. Can we tweak this to clarify things?

This revision is now accepted and ready to land.Jan 23 2023, 10:21 AM

Also I think we should mention this API in our docs, right where the removed original used to be.

Hahnfeld updated this revision to Diff 491522.Jan 23 2023, 3:21 PM

Update comment.

Hahnfeld marked an inline comment as done.Jan 23 2023, 3:25 PM

Also I think we should mention this API in our docs, right where the removed original used to be.

Which API docs are you referring to? I'm only aware of the pp-trace docs (updated in https://reviews.llvm.org/D125258 for the removal), but I didn't add FileNotFound to pp-trace because it's more of a hook than a "traceable event" - should I add it nevertheless?

clang/include/clang/Lex/PPCallbacks.h
87

Please let me know if this sounds better.

This revision was landed with ongoing or failed builds.Jan 24 2023, 12:52 AM
This revision was automatically updated to reflect the committed changes.
Hahnfeld marked an inline comment as done.

I checked a second time and couldn't find other API docs. I've now pushed this change for now (also to get it included in release/16.x), but please let me know for any follow-ups. Thanks!

Yes, I meant the pp-trace docs. Why would "we didn't find the included file" not be a traceable event?

Yes, I meant the pp-trace docs. Why would "we didn't find the included file" not be a traceable event?

My thinking is the following: If the preprocessor cannot find the included file and the Hook doesn't return true, it will run into a fatal error and stop parsing the source file. I don't know if that's actually a relevant case, but I have no experience with pp-trace, so maybe?