From 9589c1062c03fe5140dd07c1b89304d894de9d7e Mon Sep 17 00:00:00 2001 From: Scott Fennell Date: Mon, 19 Apr 2021 19:34:17 -0500 Subject: [PATCH] in ICG: Add FileSkipped preprocessor callback to FindTrickICG (#1125) * #608 add implementation of FileSkipped callback to FindTrickICG to add include chains for headers that have already been preprocessed * #608 add test SIM for FindTrickICG offsets SIM_test_icg_file_skipped --- .../RUN_test/unit_test.py | 2 ++ test/SIM_test_icg_file_skipped/S_define | 34 +++++++++++++++++++ test/SIM_test_icg_file_skipped/S_overrides.mk | 4 +++ test/SIM_test_icg_file_skipped/models/Bar.hh | 22 ++++++++++++ test/SIM_test_icg_file_skipped/models/Baz.hh | 22 ++++++++++++ test/SIM_test_icg_file_skipped/models/Foo.hh | 15 ++++++++ test/makefile | 1 + .../Interface_Code_Gen/FindTrickICG.cpp | 32 +++++++++++++++++ .../Interface_Code_Gen/FindTrickICG.hh | 11 ++++++ 9 files changed, 143 insertions(+) create mode 100644 test/SIM_test_icg_file_skipped/RUN_test/unit_test.py create mode 100644 test/SIM_test_icg_file_skipped/S_define create mode 100644 test/SIM_test_icg_file_skipped/S_overrides.mk create mode 100644 test/SIM_test_icg_file_skipped/models/Bar.hh create mode 100644 test/SIM_test_icg_file_skipped/models/Baz.hh create mode 100644 test/SIM_test_icg_file_skipped/models/Foo.hh diff --git a/test/SIM_test_icg_file_skipped/RUN_test/unit_test.py b/test/SIM_test_icg_file_skipped/RUN_test/unit_test.py new file mode 100644 index 00000000..20fbadac --- /dev/null +++ b/test/SIM_test_icg_file_skipped/RUN_test/unit_test.py @@ -0,0 +1,2 @@ + +trick.stop(3.0); diff --git a/test/SIM_test_icg_file_skipped/S_define b/test/SIM_test_icg_file_skipped/S_define new file mode 100644 index 00000000..88126cfe --- /dev/null +++ b/test/SIM_test_icg_file_skipped/S_define @@ -0,0 +1,34 @@ +/************************TRICK HEADER************************* +PURPOSE: (Test that ICG generates the correct offsets for compat15 headers even +When they are skipped by header guard optimization. This is implemented in +ICG's FindTrickICG class) +LIBRARY DEPENDENCIES: +*************************************************************/ + +#include "sim_objects/default_trick_sys.sm" + +##include "Bar.hh" +##include "Baz.hh" + +class TestFileSkippedSimObject : public Trick::SimObject { + + public: + + Bar bar; + Baz baz; + + TestFileSkippedSimObject() { + ("initialization") trick_ret = bar.test_bar_member_o_correct_offset(); + ("initialization") trick_ret = baz.test_baz_member_m_correct_offset(); + ("default_data") trick_ret = bar.test_bar_member_o_correct_offset(); + ("default_data") trick_ret = baz.test_baz_member_m_correct_offset(); + (0.25, "scheduled") trick_ret = bar.test_bar_member_o_correct_offset(); + (0.25, "scheduled") trick_ret = baz.test_baz_member_m_correct_offset(); + ("shutdown") trick_ret = bar.test_bar_member_o_correct_offset(); + ("shutdown") trick_ret = baz.test_baz_member_m_correct_offset(); + } +}; + +TestFileSkippedSimObject testfsSimObject; + + diff --git a/test/SIM_test_icg_file_skipped/S_overrides.mk b/test/SIM_test_icg_file_skipped/S_overrides.mk new file mode 100644 index 00000000..9f0dabcb --- /dev/null +++ b/test/SIM_test_icg_file_skipped/S_overrides.mk @@ -0,0 +1,4 @@ + +TRICK_CFLAGS += -I./models +TRICK_CXXFLAGS += -I./models +TRICK_ICGFLAGS = "--print-TRICK-ICG=0" diff --git a/test/SIM_test_icg_file_skipped/models/Bar.hh b/test/SIM_test_icg_file_skipped/models/Bar.hh new file mode 100644 index 00000000..7d3ec27a --- /dev/null +++ b/test/SIM_test_icg_file_skipped/models/Bar.hh @@ -0,0 +1,22 @@ +// @trick_parse{everything} +#include "Foo.hh" +#include "trick/memorymanager_c_intf.h" + +class Bar { + public: + Foo foo; + int o; + + Bar():o(777){} + + int test_bar_member_o_correct_offset() { + char ref_name[] = "testfsSimObject.bar.o"; + REF2* ref = ref_attributes(ref_name); + if(*((int*)ref->address) == o) { + return 0; + } + else { + return 1; + } + } +}; diff --git a/test/SIM_test_icg_file_skipped/models/Baz.hh b/test/SIM_test_icg_file_skipped/models/Baz.hh new file mode 100644 index 00000000..f77b24d1 --- /dev/null +++ b/test/SIM_test_icg_file_skipped/models/Baz.hh @@ -0,0 +1,22 @@ +// @trick_parse{everything} +#include "Foo.hh" +#include "trick/memorymanager_c_intf.h" + +class Baz { + public: + Foo foo; + int m; + + Baz(): m(42) {} + + int test_baz_member_m_correct_offset() { + char ref_name[] = "testfsSimObject.baz.m"; + REF2* ref = ref_attributes(ref_name); + if(*((int*)ref->address) == m) { + return 0; + } + else { + return 1; + } + } +}; diff --git a/test/SIM_test_icg_file_skipped/models/Foo.hh b/test/SIM_test_icg_file_skipped/models/Foo.hh new file mode 100644 index 00000000..f8e1c808 --- /dev/null +++ b/test/SIM_test_icg_file_skipped/models/Foo.hh @@ -0,0 +1,15 @@ +// @trick_parse{everything} +#ifndef FOO_HH +#define FOO_HH + +class Foo { +public: + #ifndef TRICK_ICG + int i; + int j; + int k; + Foo() : i(1), j(2), k(3) {} + #endif +}; + +#endif diff --git a/test/makefile b/test/makefile index 36fb2b0c..6556eeb5 100644 --- a/test/makefile +++ b/test/makefile @@ -26,6 +26,7 @@ SIMS_TO_COMPILE_AND_RUN = \ SIM_stls \ SIM_test_dp \ SIM_test_dr \ + SIM_test_icg_file_skipped \ SIM_test_io \ SIM_test_ip \ SIM_test_sched \ diff --git a/trick_source/codegen/Interface_Code_Gen/FindTrickICG.cpp b/trick_source/codegen/Interface_Code_Gen/FindTrickICG.cpp index d081518c..eab21e2a 100644 --- a/trick_source/codegen/Interface_Code_Gen/FindTrickICG.cpp +++ b/trick_source/codegen/Interface_Code_Gen/FindTrickICG.cpp @@ -35,6 +35,38 @@ void FindTrickICG::FileChanged(clang::SourceLocation Loc, FileChangeReason Reaso } } +#if (LIBCLANG_MAJOR < 10) // TODO delete when RHEL 7 no longer supported +void FindTrickICG::FileSkipped(const clang::FileEntry &SkippedFile, + const clang::Token &FilenameTok, + clang::SrcMgr::CharacteristicKind FileType) { + std::string file_name = SkippedFile.getName() ; +#else +void FindTrickICG::FileSkipped(const clang::FileEntryRef & SkippedFile, const clang::Token & FilenameTok, + clang::SrcMgr::CharacteristicKind FileType) { + /* Files that have header guards are only preprocessed once because of an optimization. + We still need to add its include chain to compat15 if TRICK_ICG was found when it was + originally preprocessed */ + std::string file_name = SkippedFile.getName().str() ; +#endif + std::string file_path; + { + char* path_cstr = almostRealPath(file_name.c_str()); + if(path_cstr != NULL) { + file_path = std::string(path_cstr); + free(path_cstr); + } + } + + // Check if skipped header is in Compat15 + if(hsd.isPathInCompat15(file_path)) { + // for each header in the stack, mark them as being exposed to TRICK_ICG + for (std::string& file : included_files) { + hsd.addTrickICGFoundFile(file); + } + } +} + + #if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5)) void FindTrickICG::If(clang::SourceLocation Loc, clang::SourceRange ConditionRange, clang::PPCallbacks::ConditionValueKind ConditionValue) #else diff --git a/trick_source/codegen/Interface_Code_Gen/FindTrickICG.hh b/trick_source/codegen/Interface_Code_Gen/FindTrickICG.hh index 157fa7a7..02160d60 100644 --- a/trick_source/codegen/Interface_Code_Gen/FindTrickICG.hh +++ b/trick_source/codegen/Interface_Code_Gen/FindTrickICG.hh @@ -20,6 +20,17 @@ class FindTrickICG : public clang::PPCallbacks { clang::SrcMgr::CharacteristicKind FileType, clang::FileID PrevFID = clang::FileID()) ; +#if (LIBCLANG_MAJOR < 10) // TODO delete when RHEL 7 no longer supported + virtual void FileSkipped(const clang::FileEntry &SkippedFile, + const clang::Token &FilenameTok, + clang::SrcMgr::CharacteristicKind FileType) ; +#else + // called when a header file is skipped because of a header guard optimization. + virtual void FileSkipped(const clang::FileEntryRef & SkippedFile, + const clang::Token & FilenameTok, + clang::SrcMgr::CharacteristicKind FileType) ; +#endif + // callbacks called when the preprocessor directives of types are processed. #if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5)) virtual void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange, clang::PPCallbacks::ConditionValueKind ConditionValue) ;