2015-02-26 15:02:31 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2016-03-16 18:24:59 +00:00
|
|
|
#include <sys/stat.h>
|
2015-02-26 15:02:31 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Basic/TargetOptions.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Parse/ParseAST.h"
|
|
|
|
|
|
|
|
#include "ICGASTConsumer.hh"
|
|
|
|
#include "HeaderSearchDirs.hh"
|
|
|
|
#include "CommentSaver.hh"
|
|
|
|
#include "TranslationUnitVisitor.hh"
|
|
|
|
#include "PrintAttributes.hh"
|
|
|
|
#include "PrintAttributesFactory.hh"
|
|
|
|
#include "Utilities.hh"
|
|
|
|
|
|
|
|
/* Command line arguments. These work better as globals, as suggested in llvm/CommandLine documentation */
|
|
|
|
llvm::cl::list< std::string > include_dirs("I", llvm::cl::Prefix, llvm::cl::desc("Include directory"),
|
|
|
|
llvm::cl::value_desc("directory")) ;
|
|
|
|
llvm::cl::list< std::string > defines("D", llvm::cl::Prefix, llvm::cl::desc("Defines"),
|
|
|
|
llvm::cl::value_desc("define")) ;
|
2016-05-05 19:49:49 +00:00
|
|
|
llvm::cl::opt< bool > units_truth_is_scary ("units-truth-is-scary", llvm::cl::desc("Don't print units conversion messages")) ;
|
2015-02-26 15:02:31 +00:00
|
|
|
llvm::cl::opt< bool > sim_services_flag ("s", llvm::cl::desc("Gernerate io_src for Trick core headers")) ;
|
|
|
|
llvm::cl::opt< bool > force ("f", llvm::cl::desc("Force all io_src files to be generated")) ;
|
|
|
|
llvm::cl::opt< int > attr_version ("v", llvm::cl::desc("Select version of attributes to produce. 10 and 13 are valid"), llvm::cl::init(10)) ;
|
|
|
|
llvm::cl::opt< int > debug_level ("d", llvm::cl::desc("Set debug level"), llvm::cl::init(0), llvm::cl::ZeroOrMore) ;
|
2015-06-24 22:49:14 +00:00
|
|
|
llvm::cl::opt< bool > create_map ("m", llvm::cl::desc("Create map files"), llvm::cl::init(false)) ;
|
|
|
|
llvm::cl::opt< std::string > output_dir ("o", llvm::cl::desc("Output directory")) ;
|
2015-02-26 15:02:31 +00:00
|
|
|
llvm::cl::alias ssf_alias ("sim_services" , llvm::cl::desc("Alias for -s") , llvm::cl::aliasopt(sim_services_flag)) ;
|
|
|
|
llvm::cl::alias force_alias ("force" , llvm::cl::desc("Alias for -f") , llvm::cl::aliasopt(force)) ;
|
|
|
|
llvm::cl::list< std::string > input_file_names(llvm::cl::Positional, llvm::cl::desc("<input_file>"), llvm::cl::ZeroOrMore) ;
|
|
|
|
llvm::cl::list< std::string > sink(llvm::cl::Sink, llvm::cl::ZeroOrMore) ;
|
|
|
|
llvm::cl::list< std::string > pre_compiled_headers("include", llvm::cl::Prefix, llvm::cl::desc("pre-compiled headers"),
|
|
|
|
llvm::cl::value_desc("pre_compiled_headers")) ;
|
2016-05-05 19:49:49 +00:00
|
|
|
//llvm::cl::opt< bool > show_units ("u", llvm::cl::desc("List recognized units")) ;
|
2015-02-26 15:02:31 +00:00
|
|
|
|
|
|
|
void ICG_version() {
|
|
|
|
std::cout << "Trick Interface Code Generator (ICG) 13.dev" << std::endl ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
Most of the main program is pieced together from examples on the web. We are doing the following.
|
|
|
|
|
|
|
|
-# Creating lists of include directories, defines, and input files from the command line arguments.
|
|
|
|
-# Initializing the compiler to read C++ code, and setting the compiler to think we are creating
|
|
|
|
code for the default target architecture.
|
|
|
|
-# Creating the necessary source and file managers as well as the preprocessor.
|
|
|
|
-# Adding search directories and creating #define statements for -D command line arguments.
|
|
|
|
-# Telling clang to use our ICGASTConsumer as an ASTConsumer.
|
|
|
|
-# Parse the input file.
|
|
|
|
|
|
|
|
*/
|
|
|
|
int main( int argc , char * argv[] ) {
|
|
|
|
|
2015-06-04 18:17:53 +00:00
|
|
|
#if (__clang_major__ >= 6) || ((__clang_major__ == 3) && (__clang_minor__ >= 5))
|
2015-02-26 15:02:31 +00:00
|
|
|
clang::TargetOptions to;
|
|
|
|
#elif (__clang_major__ == 3) && (__clang_minor__ >= 3)
|
|
|
|
clang::TargetOptions * to = new clang::TargetOptions() ;
|
|
|
|
#else
|
|
|
|
clang::TargetOptions to;
|
|
|
|
#endif
|
|
|
|
clang::CompilerInstance ci;
|
|
|
|
|
|
|
|
/* Gather all of the command line arguments into lists of include directories, defines, and input files.
|
|
|
|
All other arguments will be ignored. */
|
|
|
|
llvm::cl::SetVersionPrinter(ICG_version) ;
|
|
|
|
llvm::cl::ParseCommandLineOptions(argc , argv) ;
|
|
|
|
|
|
|
|
if ( ! validAttributesVersion(attr_version) ) {
|
|
|
|
return -1 ;
|
|
|
|
}
|
|
|
|
|
2016-05-05 19:49:49 +00:00
|
|
|
/*
|
2015-02-26 15:02:31 +00:00
|
|
|
if ( show_units ) {
|
|
|
|
list_units() ;
|
|
|
|
return 0 ;
|
|
|
|
}
|
2016-05-05 19:49:49 +00:00
|
|
|
*/
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2016-04-27 16:24:03 +00:00
|
|
|
if ( input_file_names.empty() ) {
|
|
|
|
std::cerr << "No header file specified" << std::endl ;
|
|
|
|
return 1 ;
|
|
|
|
}
|
|
|
|
|
2015-02-26 15:02:31 +00:00
|
|
|
ci.createDiagnostics();
|
|
|
|
clang::DiagnosticOptions & diago = ci.getDiagnosticOpts() ;
|
|
|
|
diago.ShowColors = 1 ;
|
|
|
|
ci.getDiagnostics().setIgnoreAllWarnings(true) ;
|
|
|
|
#if ( GCC_MAJOR == 4 ) && ( GCC_MINOR <= 2 )
|
|
|
|
ci.getDiagnostics().setSuppressAllDiagnostics() ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Set all of the defaults to c++
|
|
|
|
clang::CompilerInvocation::setLangDefaults(ci.getLangOpts() , clang::IK_CXX) ;
|
|
|
|
ci.getLangOpts().CXXExceptions = true ;
|
|
|
|
|
|
|
|
// Activate C++11 parsing
|
|
|
|
ci.getLangOpts().CPlusPlus11 = true ;
|
|
|
|
|
|
|
|
// Set the default target architecture
|
2015-06-04 18:17:53 +00:00
|
|
|
#if (__clang_major__ >= 6) || (__clang_major__ == 3) && (__clang_minor__ >= 5)
|
2015-02-26 15:02:31 +00:00
|
|
|
to.Triple = llvm::sys::getDefaultTargetTriple();
|
|
|
|
#elif (__clang_major__ == 3) && (__clang_minor__ >= 3)
|
|
|
|
to->Triple = llvm::sys::getDefaultTargetTriple();
|
|
|
|
#else
|
|
|
|
to.Triple = llvm::sys::getDefaultTargetTriple();
|
|
|
|
#endif
|
|
|
|
|
2015-06-04 18:17:53 +00:00
|
|
|
#if (__clang_major__ >= 6) || (__clang_major__ == 3) && (__clang_minor__ >= 5)
|
2015-02-26 15:02:31 +00:00
|
|
|
std::shared_ptr<clang::TargetOptions> shared_to = std::make_shared<clang::TargetOptions>(to) ;
|
|
|
|
clang::TargetInfo *pti = clang::TargetInfo::CreateTargetInfo(ci.getDiagnostics(), shared_to);
|
|
|
|
#else
|
|
|
|
clang::TargetInfo *pti = clang::TargetInfo::CreateTargetInfo(ci.getDiagnostics(), to);
|
|
|
|
#endif
|
|
|
|
ci.setTarget(pti);
|
|
|
|
|
|
|
|
// Create all of the necessary managers.
|
|
|
|
ci.createFileManager();
|
|
|
|
ci.createSourceManager(ci.getFileManager());
|
2015-06-04 18:17:53 +00:00
|
|
|
#if (__clang_major__ >= 6) || (__clang_major__ == 3) && (__clang_minor__ >= 5)
|
2015-02-26 15:02:31 +00:00
|
|
|
ci.createPreprocessor(clang::TU_Complete);
|
|
|
|
#else
|
|
|
|
ci.createPreprocessor();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
clang::HeaderSearch & hs = ci.getPreprocessor().getHeaderSearchInfo() ;
|
|
|
|
clang::HeaderSearchOptions & hso = ci.getHeaderSearchOpts() ;
|
|
|
|
clang::Preprocessor & pp = ci.getPreprocessor() ;
|
|
|
|
|
|
|
|
// Add all of the include directories to the preprocessor
|
|
|
|
HeaderSearchDirs hsd(hs , hso , pp, sim_services_flag) ;
|
|
|
|
hsd.addSearchDirs ( include_dirs ) ;
|
|
|
|
|
|
|
|
// Tell the preprocessor to use its default predefines
|
|
|
|
clang::PreprocessorOptions & ppo = ci.getPreprocessorOpts() ;
|
|
|
|
ppo.UsePredefines = true;
|
2016-04-18 20:22:52 +00:00
|
|
|
#if (__clang_major__ == 3) && (__clang_minor__ >= 8)
|
|
|
|
pp.getBuiltinInfo().initializeBuiltins(pp.getIdentifierTable(), pp.getLangOpts());
|
|
|
|
#else
|
2015-02-26 15:02:31 +00:00
|
|
|
pp.getBuiltinInfo().InitializeBuiltins(pp.getIdentifierTable(), pp.getLangOpts());
|
2016-04-18 20:22:52 +00:00
|
|
|
#endif
|
2015-02-26 15:02:31 +00:00
|
|
|
// Add all of the #define from the command line to the default predefines.
|
|
|
|
hsd.addDefines ( defines ) ;
|
|
|
|
|
|
|
|
// Add our comment saver as a comment handler in the preprocessor
|
2015-06-10 19:19:00 +00:00
|
|
|
CommentSaver cs(ci, hsd) ;
|
2015-02-26 15:02:31 +00:00
|
|
|
pp.addCommentHandler(&cs) ;
|
|
|
|
|
2015-06-24 22:49:14 +00:00
|
|
|
PrintAttributes pa( attr_version, hsd, cs, ci, force, sim_services_flag, output_dir) ;
|
2015-02-26 15:02:31 +00:00
|
|
|
|
|
|
|
// create new class and enum map files
|
2015-06-24 22:49:14 +00:00
|
|
|
if ( create_map ) {
|
|
|
|
pa.createMapFiles() ;
|
|
|
|
}
|
2015-02-26 15:02:31 +00:00
|
|
|
|
|
|
|
// Tell the compiler to use our ICGASTconsumer
|
|
|
|
ICGASTConsumer *astConsumer = new ICGASTConsumer(ci, hsd, cs, pa);
|
2015-06-04 18:17:53 +00:00
|
|
|
#if (__clang_major__ >= 6) || ((__clang_major__ == 3) && (__clang_minor__ >= 6))
|
2015-02-11 00:47:01 +00:00
|
|
|
std::unique_ptr<clang::ASTConsumer> unique_ast(astConsumer) ;
|
|
|
|
ci.setASTConsumer(std::move(unique_ast));
|
|
|
|
#else
|
2015-02-26 15:02:31 +00:00
|
|
|
ci.setASTConsumer(astConsumer);
|
2015-02-11 00:47:01 +00:00
|
|
|
#endif
|
2015-02-26 15:02:31 +00:00
|
|
|
ci.createASTContext();
|
|
|
|
ci.createSema(clang::TU_Prefix, NULL);
|
|
|
|
|
|
|
|
// Get the full path of the file to be read.
|
|
|
|
char * input_file_cp = strdup(input_file_names[0].c_str()) ;
|
|
|
|
char * input_file_file = basename(input_file_cp) ;
|
|
|
|
char * input_file_dir = dirname(input_file_cp) ;
|
|
|
|
char * input_file_full_path = NULL ;
|
|
|
|
std::stringstream os ;
|
|
|
|
os << input_file_dir << "/" << input_file_file ;
|
|
|
|
input_file_full_path = almostRealPath( os.str().c_str() ) ;
|
|
|
|
//std::cout << input_file_full_path << std::endl ;
|
|
|
|
|
2016-03-16 18:24:59 +00:00
|
|
|
struct stat buffer ;
|
|
|
|
if ( stat ( input_file_full_path , &buffer) != 0 ) {
|
|
|
|
std::cerr << "Could not open file " << input_file_full_path << std::endl ;
|
|
|
|
exit(-1) ;
|
|
|
|
}
|
2015-02-26 15:02:31 +00:00
|
|
|
// Open up the input file and parse it.
|
|
|
|
const clang::FileEntry *pFile = ci.getFileManager().getFile(input_file_full_path);
|
2015-06-04 18:17:53 +00:00
|
|
|
#if (__clang_major__ >= 6) || ((__clang_major__ == 3) && (__clang_minor__ >= 5))
|
2015-02-26 15:02:31 +00:00
|
|
|
ci.getSourceManager().setMainFileID(ci.getSourceManager().createFileID(pFile, clang::SourceLocation(), clang::SrcMgr::C_User));
|
|
|
|
#else
|
|
|
|
ci.getSourceManager().createMainFileID(pFile);
|
|
|
|
#endif
|
|
|
|
ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(), &ci.getPreprocessor());
|
|
|
|
clang::ParseAST(ci.getSema());
|
|
|
|
ci.getDiagnosticClient().EndSourceFile();
|
|
|
|
|
|
|
|
free(input_file_cp) ;
|
|
|
|
free(input_file_full_path) ;
|
|
|
|
|
2015-07-02 15:09:31 +00:00
|
|
|
if ( ! sim_services_flag ) {
|
|
|
|
pa.printIOMakefile() ;
|
|
|
|
}
|
|
|
|
|
2015-02-26 15:02:31 +00:00
|
|
|
// Close the map files
|
|
|
|
pa.closeMapFiles() ;
|
|
|
|
|
|
|
|
// Print the list of headers that have the ICG:(No) comment
|
|
|
|
pa.printICGNoFiles() ;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|