mirror of
https://github.com/nasa/trick.git
synced 2025-06-14 05:08:22 +00:00
Fix several ICG class template bugs (#1871)
* Fix several bugs with processing templates in ICG - Fixed an issue where io_src code for some templates was being placed in the wrong file. - Fixed an issue with templates being instantiated with templates as parameters. - Fixed an issue with templates that instantiate templates. Added additional tests to cover these cases. * Moved the list of template argument header dependencies from ClassValues to PrintFileContents10 so they can be printed to the io_ file regardless of the order in which class info is printed. * Moved the list of template argument header dependencies from ClassValues to PrintFileContents10 so they can be printed to the io_ file regardless of the order in which class info is printed. Moved the list of template argument header dependencies from ClassValues to PrintFileContents10 so they can be printed to the io_ file regardless of the order in which class info is printed. * Added support to handle template enum class type argument and removed some commented code. Added support to handle template enum class type argument and removed some commented code. --------- Co-authored-by: Hong Chen <hchen99@users.noreply.github.com>
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "clang/Basic/SourceManager.h"
|
||||
@ -16,6 +17,7 @@
|
||||
#include "Utilities.hh"
|
||||
#include "CommentSaver.hh"
|
||||
#include "PrintAttributes.hh"
|
||||
#include "PrintFileContents10.hh"
|
||||
#include "BraceMacro.hh"
|
||||
|
||||
extern llvm::cl::opt< int > debug_level ;
|
||||
@ -229,7 +231,7 @@ bool CXXRecordVisitor::VisitCXXRecordDecl( clang::CXXRecordDecl *rec ) {
|
||||
cval.setPOD(rec->isPOD()) ;
|
||||
|
||||
cval.setSize(rec->getASTContext().getASTRecordLayout(rec).getSize().getQuantity()) ;
|
||||
|
||||
addTemplateArgumentDependencies(rec);
|
||||
|
||||
//std::cout << "parsing " << cval.getName() << std::endl ;
|
||||
//std::cout << " [34mprocessing inheritance " << rec->getNumBases() << " " << rec->getNumVBases() << "[00m" << std::endl ;
|
||||
@ -419,3 +421,55 @@ bool CXXRecordVisitor::isPrivateEmbeddedClass( std::string in_name ) {
|
||||
}
|
||||
return false ;
|
||||
}
|
||||
|
||||
void CXXRecordVisitor::addTemplateArgumentDependencies(const clang::CXXRecordDecl *rec) {
|
||||
if (cval.getFileName() == "S_source.hh" ||
|
||||
!clang::ClassTemplateSpecializationDecl::classof(rec)) {
|
||||
return;
|
||||
}
|
||||
const auto * ctd = clang::cast<clang::ClassTemplateSpecializationDecl>(rec);
|
||||
|
||||
// Iterate over all template arguments. If any of these arguments come from an
|
||||
// external header file, make sure to #include it when generating this class's
|
||||
// io_src file.
|
||||
for (const auto & arg : ctd->getTemplateArgs().asArray()) {
|
||||
if (arg.getKind() != clang::TemplateArgument::ArgKind::Type ||
|
||||
!arg.getAsType().getTypePtrOrNull()) {
|
||||
continue;
|
||||
}
|
||||
const auto * type_ptr = arg.getAsType().getTypePtr();
|
||||
|
||||
clang::TagDecl * arg_decl = nullptr;
|
||||
switch (type_ptr->getTypeClass()) {
|
||||
case clang::Type::Record:
|
||||
arg_decl = type_ptr->getAsCXXRecordDecl();
|
||||
break;
|
||||
case clang::Type::Enum: {
|
||||
// Handle enum or enum class
|
||||
const clang::EnumType *et = type_ptr->getAs<clang::EnumType>();
|
||||
if (et) {
|
||||
arg_decl = et->getDecl();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case clang::Type::ConstantArray:
|
||||
// Will remain nullptr if this is a builtin type.
|
||||
arg_decl = type_ptr->getPointeeOrArrayElementType()->getAsCXXRecordDecl();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!arg_decl) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string arg_header_file = getFileName(ci, arg_decl->getBraceRange().getEnd(), hsd);
|
||||
|
||||
pa.printer->addTemplateArgumentHeaderDependency(cval.getFileName(), arg_header_file);
|
||||
|
||||
if (debug_level >= 2) {
|
||||
std::cout << "\n\033[34mCXXRecordVisitor addTemplateArgumentDependencies "
|
||||
<< arg_header_file << "\033[0m" << std::endl ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user