Skip to content

[llvm][Support] Add indirection to call correct validate(...) function #71966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023

Conversation

arrv-sc
Copy link
Contributor

@arrv-sc arrv-sc commented Nov 10, 2023

Previously "yamlize" overload for validatedMappingTraits was
unconditionally calling "MappingTraits<T>::validate" even if
"MappingContextTraits<T, Context>" was passed to it. Therefore
compilation failed when specifying
"MappingContextTraits<T,Context>::validate()"

@llvmbot
Copy link
Member

llvmbot commented Nov 10, 2023

@llvm/pr-subscribers-llvm-support

Author: None (arrv-sc)

Changes
Previously "yamlize" overload for validatedMappingTraits was
unconditionally calling "MappingTraits&lt;T&gt;::validate" even if
"MappingContextTraits&lt;T, Context&gt;" was passed to it. Therefore
compilation failed when specifying
"MappingContextTraits&lt;T,Context&gt;::validate()"

Full diff: https://github.com/llvm/llvm-project/pull/71966.diff

1 Files Affected:

  • (modified) llvm/include/llvm/Support/YAMLTraits.h (+15-2)
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 99074105a556989..86226ef46cf5482 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -1058,6 +1058,19 @@ yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
   }
 }
 
+template <typename T, typename Context,
+          std::enable_if_t<validatedMappingTraits<T, Context>::value, int> = 0>
+std::string callValidate(IO &io, T &Val, Context &Ctx) {
+  return MappingContextTraits<T, Context>::validate(io, Val, Ctx);
+}
+
+template <
+    typename T,
+    std::enable_if_t<validatedMappingTraits<T, EmptyContext>::value, int> = 0>
+std::string callValidate(IO &io, T &Val, EmptyContext = {}) {
+  return MappingTraits<T>::validate(io, Val);
+}
+
 template <typename T, typename Context>
 std::enable_if_t<validatedMappingTraits<T, Context>::value, void>
 yamlize(IO &io, T &Val, bool, Context &Ctx) {
@@ -1066,7 +1079,7 @@ yamlize(IO &io, T &Val, bool, Context &Ctx) {
   else
     io.beginMapping();
   if (io.outputting()) {
-    std::string Err = MappingTraits<T>::validate(io, Val);
+    std::string Err = callValidate(io, Val, Ctx);
     if (!Err.empty()) {
       errs() << Err << "\n";
       assert(Err.empty() && "invalid struct trying to be written as yaml");
@@ -1074,7 +1087,7 @@ yamlize(IO &io, T &Val, bool, Context &Ctx) {
   }
   detail::doMapping(io, Val, Ctx);
   if (!io.outputting()) {
-    std::string Err = MappingTraits<T>::validate(io, Val);
+    std::string Err = callValidate(io, Val, Ctx);
     if (!Err.empty())
       io.setError(Err);
   }

@arrv-sc
Copy link
Contributor Author

arrv-sc commented Nov 16, 2023

Small patch that makes it possible to use validate(....) in llvm::yaml::ContextMappingTraits.

@arrv-sc
Copy link
Contributor Author

arrv-sc commented Nov 16, 2023

@RKSimon could you please take a look?

@arrv-sc
Copy link
Contributor Author

arrv-sc commented Nov 20, 2023

@aaupov Could you take a look please?

@aaupov aaupov removed their request for review November 21, 2023 11:01
@aaupov
Copy link
Contributor

aaupov commented Nov 21, 2023

I'm sorry but I'm unfamiliar with the code.

Copy link

github-actions bot commented Nov 26, 2023

:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.

@arrv-sc
Copy link
Contributor Author

arrv-sc commented Nov 30, 2023

@RKSimon, @topperc, take a look please

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

@arrv-sc
Copy link
Contributor Author

arrv-sc commented Dec 1, 2023

@topperc

@arrv-sc
Copy link
Contributor Author

arrv-sc commented Dec 4, 2023

@topperc, please, review

@arrv-sc
Copy link
Contributor Author

arrv-sc commented Dec 14, 2023

I would highly appreciate you taking a look on this PR @topperc

@topperc
Copy link
Collaborator

topperc commented Dec 14, 2023

Is this a similar problem to what detail::doMapping is solving for the mapping function? Perhaps we could structure this the same way for consistency?

    Previously "yamlize" overload for validatedMappingTraits was
    unconditionally calling "MappingTraits<T>::validate" even if
    "MappingContextTraits<T, Context>" was passed to it. Therefore
    compilation failed when specifying
    "MappingContextTraits<T,Context>::validate()"
@arrv-sc
Copy link
Contributor Author

arrv-sc commented Dec 14, 2023

Is this a similar problem to what detail::doMapping is solving for the mapping function? Perhaps we could structure this the same way for consistency?

I actually agree with you. Updated.

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arrv-sc
Copy link
Contributor Author

arrv-sc commented Dec 18, 2023

@RKSimon, merge?

@RKSimon
Copy link
Collaborator

RKSimon commented Dec 18, 2023

@RKSimon, merge?

LGTM - do you mean you want me to merge it for you?

@arrv-sc
Copy link
Contributor Author

arrv-sc commented Dec 18, 2023

LGTM - do you mean you want me to merge it for you?

Yes. Sorry if I'm not clear

@RKSimon RKSimon merged commit 74cf525 into llvm:main Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants