Skip to content

Commit

Permalink
Fix the chicken-and-egg problem where you use type hashes generated from
Browse files Browse the repository at this point in the history
types in the same library:

1) clscan parses your C++ file.
2) Your C++ file includes clcppcodegen.h.
3) OOPS! That file is generated by step 5 and doesn't exist yet.
4) clmerge parses the output from clscan.
5) clmerge generates clcppcodegen.h.
  • Loading branch information
dwilliamson committed Apr 12, 2021
1 parent b689a5c commit 7e4800e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
24 changes: 23 additions & 1 deletion inc/clcpp/clcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -984,5 +984,27 @@ namespace clcpp
}
}

//
// Compile-time typename hash
//
#ifdef __clcpp_parse__

// This path is only compiled during a clscan pass and its intention is to ensure a unique value is returned from clcppTypeHash
// without needing to include the generated header file. This solves the issue of requiring the generated header file to allow
// clscan to compile successfully, before the dependent process clmerge generates the header file using the output of clscan.
constexpr unsigned int clcppCompileTimeHash(const char* const str, const unsigned int value)
{
return str[0] == 0 ? value : clcppCompileTimeHash(str + 1, (value ^ (unsigned int)str[0]) * 0x01000193);
}
template <typename Type>
constexpr unsigned int clcppTypeHash();
constexpr unsigned int clcppTypeHash()
{
return clcppCompileTimeHash(__PRETTY_FUNCTION__, 0x01000193);
}

#else

template <typename Type>
constexpr unsigned int clcppTypeHash();

#endif
6 changes: 5 additions & 1 deletion src/clReflectTest/TestConstexprGetType.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

#include "clcppcodegen.h"
#include <clcpp/clcpp.h>
#include <cstdio>

#ifndef __clcpp_parse__
#include "clcppcodegen.h"
#endif

struct Base
{
const clcpp::Type* type = nullptr;
Expand Down

0 comments on commit 7e4800e

Please sign in to comment.