Skip to content
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

generated hpp file is not #included by generated cpp file [BUG] #1145

Open
GrahamAsher opened this issue Jul 1, 2024 · 3 comments
Open

generated hpp file is not #included by generated cpp file [BUG] #1145

GrahamAsher opened this issue Jul 1, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@GrahamAsher
Copy link

The current system for creating and using classes outside the main cpp2 file is to write a file with the extension .h2 containing the classes that are needed, then put #include "myheader.h2" at the top of the cpp2 file. The idea is that

  1. cppfront creates two files from the h2 file: a .h file containing declarations, and a .hpp file containing definitions.
  2. cppfront translates #include "myheader.h2" in the cpp2 file into two statements in the generated cpp file: #include "myheader.h" at the top, and ...
  3. #include "myheader.hpp" later on.

Steps 1 and 2 work, but step 3 does not. No #include "myheader.hpp" statement is added by cppfront to the generated cpp file.

Suggested fix (works for me, but I have not analysed the code and the fix may not be correct in all circumstances): move the statement printer.print_extra( hpp_includes ); in to_cpp1.hpp out of its conditional block. Here is a patch:

 source/to_cpp1.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/source/to_cpp1.h b/source/to_cpp1.h
index 75880d5..b7f713d 100644
--- a/source/to_cpp1.h
+++ b/source/to_cpp1.h
@@ -1519,9 +1519,9 @@ public:
             printer.print_extra( "\n#ifndef " + cpp1_FILENAME+"_CPP2" );
             printer.print_extra( "\n#define " + cpp1_FILENAME+"_CPP2" + "\n\n" );
 
-            printer.print_extra( hpp_includes );
         }
 
+        printer.print_extra(hpp_includes);
 
         //---------------------------------------------------------------------
         //  Do phase2_func_defs
@GrahamAsher GrahamAsher added the bug Something isn't working label Jul 1, 2024
@GrahamAsher
Copy link
Author

GrahamAsher commented Jul 1, 2024

Although I suspect that my idea of how it works is wrong, because it would mean that if multiple cpp2 files included a .h2 file, the generated cpp files would each contain all the definitions in the generated hpp file; so I'm not sure what is meant to happen.

However, in the current design it seems that only one cpp2 file can be used in an app, so the system is workable if that single cpp2 file gives rise to a single cpp file in which all the hpp files are #included.

@GrahamAsher
Copy link
Author

Revised patch: add a check that the current output file isn't a .h file, emit an explanatory comment, and move the code after emitting function definitions:

 source/to_cpp1.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/source/to_cpp1.h b/source/to_cpp1.h
index 75880d5..a931a17 100644
--- a/source/to_cpp1.h
+++ b/source/to_cpp1.h
@@ -1519,10 +1519,8 @@ public:
             printer.print_extra( "\n#ifndef " + cpp1_FILENAME+"_CPP2" );
             printer.print_extra( "\n#define " + cpp1_FILENAME+"_CPP2" + "\n\n" );
 
-            printer.print_extra( hpp_includes );
         }
 
-
         //---------------------------------------------------------------------
         //  Do phase2_func_defs
         //
@@ -1554,6 +1552,12 @@ public:
             printer.print_extra( "\n#endif" );
         }
 
+        // Emit code to #include the .hpp files generated from .h2 files.
+        if (cpp1_filename.back() != 'h') {
+            printer.print_extra("\n// include .hpp files generated from .h2 files \n");
+            printer.print_extra(hpp_includes);
+        }
+
         printer.finalize_phase( true );
 
         //  Finally, some debug checks

@adam-ce
Copy link

adam-ce commented Jul 18, 2024

i believe this is a duplicate of #594

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants