hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.24k stars 223 forks source link

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

Open GrahamAsher opened 6 days ago

GrahamAsher commented 6 days ago

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 commented 6 days ago

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 commented 4 days ago

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