getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML / msgpack.org[C++20]
https://getml.github.io/reflect-cpp/
MIT License
900 stars 76 forks source link

Missing header includes in some files #41

Closed GregTheMadMonk closed 8 months ago

GregTheMadMonk commented 8 months ago

My example program and compile log (reflect-cpp is cloned from main):

[greg@greg-spc-arch build]$ bat ../CMakeLists.txt ../main.cc
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: ../CMakeLists.txt
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ cmake_minimum_required( VERSION 3.28 )
   2   │
   3   │ project( TestRefl )
   4   │
   5   │ set( CMAKE_CXX_STANDARD 20 )
   6   │ add_subdirectory( reflect-cpp )
   7   │ set( REFLECTCPP_JSON ON )
   8   │
   9   │ add_executable( main main.cc )
  10   │ target_link_libraries( main PRIVATE reflectcpp )
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: ../main.cc
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ #include <rfl/json.hpp>
   2   │ #include <rfl.hpp>
   3   │
   4   │ #include <iostream>
   5   │
   6   │ struct S { int a; float b; };
   7   │
   8   │ int main() {
   9   │     S s{};
  10   │     std::cout << rfl::json::write(s) << '\n';
  11   │ }
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
[greg@greg-spc-arch build]$ cmake .. && make
-- The C compiler identification is GNU 13.2.1
-- The CXX compiler identification is GNU 13.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.5s)
-- Generating done (0.0s)
-- Build files have been written to: /home/greg/tmp/reproduce/build
[ 25%] Building C object reflect-cpp/CMakeFiles/reflectcpp.dir/src/yyjson.c.o
[ 50%] Linking C static library libreflectcpp.a
[ 50%] Built target reflectcpp
[ 75%] Building CXX object CMakeFiles/main.dir/main.cc.o
In file included from /home/greg/tmp/reproduce/reflect-cpp/include/rfl/parsing/Parser_default.hpp:13,
                 from /home/greg/tmp/reproduce/reflect-cpp/include/rfl/parsing/Parser.hpp:7,
                 from /home/greg/tmp/reproduce/reflect-cpp/include/rfl/json/Parser.hpp:6,
                 from /home/greg/tmp/reproduce/reflect-cpp/include/rfl/json.hpp:4,
                 from /home/greg/tmp/reproduce/main.cc:1:
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/internal/is_attribute.hpp:20:20: error: ‘Attribute’ was not declared in this scope; did you mean ‘is_attribute’?
   20 | class is_attribute<Attribute<Type>> : public std::true_type {};
      |                    ^~~~~~~~~
      |                    is_attribute
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/internal/is_attribute.hpp:20:34: error: template argument 1 is invalid
   20 | class is_attribute<Attribute<Type>> : public std::true_type {};
      |                                  ^~
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/internal/is_attribute.hpp:23:20: error: ‘Attribute’ was not declared in this scope; did you mean ‘is_attribute’?
   23 | class is_attribute<Attribute<Type>*> : public std::true_type {};
      |                    ^~~~~~~~~
      |                    is_attribute
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/internal/is_attribute.hpp:23:34: error: template argument 1 is invalid
   23 | class is_attribute<Attribute<Type>*> : public std::true_type {};
      |                                  ^
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/internal/is_attribute.hpp:23:36: error: expected unqualified-id before ‘>’ token
   23 | class is_attribute<Attribute<Type>*> : public std::true_type {};
      |                                    ^
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/internal/is_attribute.hpp:26:40: error: ISO C++ forbids declaration of ‘type name’ with no type [-fpermissive]
   26 | class is_attribute<const Attribute<Type>*> : public std::true_type {};
      |                                        ^
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/internal/is_attribute.hpp:26:42: error: template argument 1 is invalid
   26 | class is_attribute<const Attribute<Type>*> : public std::true_type {};
      |                                          ^
In file included from /home/greg/tmp/reproduce/reflect-cpp/include/rfl/parsing/Parser_named_tuple.hpp:4,
                 from /home/greg/tmp/reproduce/reflect-cpp/include/rfl/parsing/Parser.hpp:9:
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/parsing/NamedTupleParser.hpp:214:27: error: ‘Memoization’ in namespace ‘rfl::internal’ does not name a template type
  214 |   static inline internal::Memoization<
      |                           ^~~~~~~~~~~
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/parsing/NamedTupleParser.hpp: In static member function ‘static const auto& rfl::parsing::NamedTupleParser<R, W, _ignore_empty_containers, FieldTypes>::field_indices()’:
/home/greg/tmp/reproduce/reflect-cpp/include/rfl/parsing/NamedTupleParser.hpp:177:12: error: ‘field_indices_’ was not declared in this scope; did you mean ‘field_indices’?
  177 |     return field_indices_.value(set_field_indices<0>);
      |            ^~~~~~~~~~~~~~
      |            field_indices
make[2]: *** [CMakeFiles/main.dir/build.make:76: CMakeFiles/main.dir/main.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
liuzicheng1987 commented 8 months ago

Interesting. The only possible explanation I have is that you put rfl/json.hpp before rfl.hpp. But I agree, it shouldn’t be necessary.

Thanks for the issue. I will take a look.

ChemiCalChems commented 8 months ago

I also noticed this using clangd on my machine. It seems the tests compile fine because they all include stuff previously, but I would expect everything to be internally consistent too. I have an idea to check for consistency with a Github Actions check: try compile every single header on its own and see if it breaks or not. clang-tidy might also be an option, I'll check.

liuzicheng1987 commented 8 months ago

By the way, here is what happened: As suspected, the issue was resolved once you change the include order of rfl.hpp and rfl/json.hpp.

I use clang-format which does that for me automatically. I agree with @ChemiCalChems ...something like clang-tidy would be a good idea to avoid this in the future.

Anyway, @GregTheMadMonk , thanks for the issue.