Renamed most variabes so that they are self-descriptive. Stop using abbreviations! No one can tell what anything is.

Applied a consistent formatting.
Inlined several one-off variables.
This commit is contained in:
Derek Bankieris 2016-10-21 15:06:44 -05:00
parent 108d3d40c5
commit 015965abab

View File

@ -25,185 +25,159 @@
#include "PrintAttributesFactory.hh" #include "PrintAttributesFactory.hh"
#include "Utilities.hh" #include "Utilities.hh"
/* Command line arguments. These work better as globals, as suggested in llvm/CommandLine documentation */ // 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::list<std::string> include_dirs("I", llvm::cl::Prefix, llvm::cl::desc("Include directory"), llvm::cl::value_desc("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"));
llvm::cl::list< std::string > defines("D", llvm::cl::Prefix, llvm::cl::desc("Defines"), llvm::cl::opt<bool> units_truth_is_scary("units-truth-is-scary", llvm::cl::desc("Don't print units conversion messages"));
llvm::cl::value_desc("define")) ; llvm::cl::opt<bool> sim_services_flag("s", llvm::cl::desc("Gernerate io_src for Trick core headers"));
llvm::cl::opt< bool > units_truth_is_scary ("units-truth-is-scary", llvm::cl::desc("Don't print units conversion messages")) ; llvm::cl::opt<bool> force("f", llvm::cl::desc("Force all io_src files to be generated"));
llvm::cl::opt< bool > sim_services_flag ("s", llvm::cl::desc("Gernerate io_src for Trick core headers")) ; 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< bool > force ("f", llvm::cl::desc("Force all io_src files to be generated")) ; llvm::cl::opt<int> debug_level("d", llvm::cl::desc("Set debug level"), llvm::cl::init(0), llvm::cl::ZeroOrMore);
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<bool> create_map("m", llvm::cl::desc("Create map files"), llvm::cl::init(false));
llvm::cl::opt< int > debug_level ("d", llvm::cl::desc("Set debug level"), llvm::cl::init(0), llvm::cl::ZeroOrMore) ; llvm::cl::opt<std::string> output_dir("o", llvm::cl::desc("Output directory"));
llvm::cl::opt< bool > create_map ("m", llvm::cl::desc("Create map files"), llvm::cl::init(false)) ; llvm::cl::alias ssf_alias("sim_services" , llvm::cl::desc("Alias for -s") , llvm::cl::aliasopt(sim_services_flag));
llvm::cl::opt< std::string > output_dir ("o", llvm::cl::desc("Output directory")) ; llvm::cl::alias force_alias("force" , llvm::cl::desc("Alias for -f") , llvm::cl::aliasopt(force));
llvm::cl::alias ssf_alias ("sim_services" , llvm::cl::desc("Alias for -s") , llvm::cl::aliasopt(sim_services_flag)) ; llvm::cl::list<std::string> input_file_names(llvm::cl::Positional, llvm::cl::desc("<input_file>"), llvm::cl::ZeroOrMore);
llvm::cl::alias force_alias ("force" , llvm::cl::desc("Alias for -f") , llvm::cl::aliasopt(force)) ; llvm::cl::list<std::string> sink(llvm::cl::Sink, llvm::cl::ZeroOrMore);
llvm::cl::list< std::string > input_file_names(llvm::cl::Positional, llvm::cl::desc("<input_file>"), 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"));
llvm::cl::list< std::string > sink(llvm::cl::Sink, llvm::cl::ZeroOrMore) ; llvm::cl::opt<bool> no_offset_of("n", llvm::cl::desc("Do not print the offsetof calculations in attributes"));
llvm::cl::list< std::string > pre_compiled_headers("include", llvm::cl::Prefix, llvm::cl::desc("pre-compiled headers"), llvm::cl::alias no_offset_of_alias("no-offset-of" , llvm::cl::desc("Alias for -n") , llvm::cl::aliasopt(no_offset_of));
llvm::cl::value_desc("pre_compiled_headers")) ;
llvm::cl::opt< bool > no_offset_of ("n", llvm::cl::desc("Do not print the offsetof calculations in attributes")) ;
llvm::cl::alias no_offset_of_alias ("no-offset-of" , llvm::cl::desc("Alias for -n") , llvm::cl::aliasopt(no_offset_of)) ;
//llvm::cl::opt< bool > show_units ("u", llvm::cl::desc("List recognized units")) ;
void ICG_version() {
std::cout << "Trick Interface Code Generator (trick-ICG) " << TRICK_VERSION << std::endl ;
}
/** /**
Most of the main program is pieced together from examples on the web. We are doing the following:
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. -# 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 -# Initializing the compiler to read C++ code, and setting the compiler to think we are creating code for the default target architecture.
code for the default target architecture.
-# Creating the necessary source and file managers as well as the preprocessor. -# Creating the necessary source and file managers as well as the preprocessor.
-# Adding search directories and creating #define statements for -D command line arguments. -# Adding search directories and creating #define statements for -D command line arguments.
-# Telling clang to use our ICGASTConsumer as an ASTConsumer. -# Telling clang to use our ICGASTConsumer as an ASTConsumer.
-# Parse the input file. -# Parsing the input file.
*/ */
int main( int argc , char * argv[] ) { int main(int argc, char * argv[]) {
llvm::cl::SetVersionPrinter([] {
std::cout << "Trick Interface Code Generator (trick-ICG) " << TRICK_VERSION << std::endl;
});
#if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5)) /**
clang::TargetOptions to; * Gather all of the command line arguments into lists of include directories, defines, and input files.
#else * All other arguments will be ignored.
clang::TargetOptions * to = new clang::TargetOptions() ; */
#endif llvm::cl::ParseCommandLineOptions(argc, argv);
clang::CompilerInstance ci;
/* Gather all of the command line arguments into lists of include directories, defines, and input files. if (!validAttributesVersion(attr_version)) {
All other arguments will be ignored. */ return -1;
llvm::cl::SetVersionPrinter(ICG_version) ;
llvm::cl::ParseCommandLineOptions(argc , argv) ;
if ( ! validAttributesVersion(attr_version) ) {
return -1 ;
} }
if ( input_file_names.empty() ) { if (input_file_names.empty()) {
std::cerr << "No header file specified" << std::endl ; std::cerr << "No header file specified" << std::endl;
return 1 ; return 1;
} }
ci.createDiagnostics(); clang::CompilerInstance compilerInstance;
clang::DiagnosticOptions & diago = ci.getDiagnosticOpts() ; compilerInstance.createDiagnostics();
diago.ShowColors = 1 ; compilerInstance.getDiagnosticOpts().ShowColors = 1;
ci.getDiagnostics().setIgnoreAllWarnings(true) ; compilerInstance.getDiagnostics().setIgnoreAllWarnings(true);
// Set all of the defaults to c++ // Set all of the defaults to c++
clang::CompilerInvocation::setLangDefaults(ci.getLangOpts() , clang::IK_CXX) ; clang::CompilerInvocation::setLangDefaults(compilerInstance.getLangOpts(), clang::IK_CXX);
ci.getLangOpts().CXXExceptions = true ; compilerInstance.getLangOpts().CXXExceptions = true;
// Activate C++11 parsing // Activate C++11 parsing
ci.getLangOpts().CPlusPlus11 = true ; compilerInstance.getLangOpts().CPlusPlus11 = true;
// Set the default target architecture // Set the default target architecture
#if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5)) #if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5))
to.Triple = llvm::sys::getDefaultTargetTriple(); clang::TargetOptions targetOptions;
targetOptions.Triple = llvm::sys::getDefaultTargetTriple();
compilerInstance.setTarget(clang::TargetInfo::CreateTargetInfo(compilerInstance.getDiagnostics(), std::make_shared<clang::TargetOptions>(targetOptions)));
#else #else
to->Triple = llvm::sys::getDefaultTargetTriple(); clang::TargetOptions* targetOptions = new clang::TargetOptions();
targetOptions->Triple = llvm::sys::getDefaultTargetTriple();
compilerInstance.setTarget(clang::TargetInfo::CreateTargetInfo(compilerInstance.getDiagnostics(), targetOptions));
#endif #endif
// Create all of the necessary managers
compilerInstance.createFileManager();
compilerInstance.createSourceManager(compilerInstance.getFileManager());
// Create the preprocessor
#if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5)) #if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5))
std::shared_ptr<clang::TargetOptions> shared_to = std::make_shared<clang::TargetOptions>(to) ; compilerInstance.createPreprocessor(clang::TU_Complete);
clang::TargetInfo *pti = clang::TargetInfo::CreateTargetInfo(ci.getDiagnostics(), shared_to);
#else #else
clang::TargetInfo *pti = clang::TargetInfo::CreateTargetInfo(ci.getDiagnostics(), to); compilerInstance.createPreprocessor();
#endif #endif
ci.setTarget(pti); clang::Preprocessor& preprocessor = compilerInstance.getPreprocessor();
// Create all of the necessary managers.
ci.createFileManager();
ci.createSourceManager(ci.getFileManager());
#if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5))
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 // Add all of the include directories to the preprocessor
HeaderSearchDirs hsd(hs , hso , pp, sim_services_flag) ; HeaderSearchDirs headerSearchDirs(compilerInstance.getPreprocessor().getHeaderSearchInfo(), compilerInstance.getHeaderSearchOpts(), preprocessor, sim_services_flag);
hsd.addSearchDirs ( include_dirs ) ; headerSearchDirs.addSearchDirs(include_dirs);
// Tell the preprocessor to use its default predefines // Tell the preprocessor to use its default predefines
clang::PreprocessorOptions & ppo = ci.getPreprocessorOpts() ; compilerInstance.getPreprocessorOpts().UsePredefines = true;
ppo.UsePredefines = true;
#if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 8)) #if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 8))
pp.getBuiltinInfo().initializeBuiltins(pp.getIdentifierTable(), pp.getLangOpts()); preprocessor.getBuiltinInfo().initializeBuiltins(preprocessor.getIdentifierTable(), preprocessor.getLangOpts());
#else #else
pp.getBuiltinInfo().InitializeBuiltins(pp.getIdentifierTable(), pp.getLangOpts()); preprocessor.getBuiltinInfo().InitializeBuiltins(preprocessor.getIdentifierTable(), preprocessor.getLangOpts());
#endif #endif
// Add all of the #define from the command line to the default predefines. // Add all of the #define from the command line to the default predefines
hsd.addDefines ( defines ) ; headerSearchDirs.addDefines(defines);
// Add our comment saver as a comment handler in the preprocessor // Add our comment saver as a comment handler in the preprocessor
CommentSaver cs(ci, hsd) ; CommentSaver commentSaver(compilerInstance, headerSearchDirs);
pp.addCommentHandler(&cs) ; preprocessor.addCommentHandler(&commentSaver);
PrintAttributes pa( attr_version, hsd, cs, ci, force, sim_services_flag, output_dir) ; PrintAttributes printAttributes(attr_version, headerSearchDirs, commentSaver, compilerInstance, force, sim_services_flag, output_dir);
// create new class and enum map files // Create new class and enum map files
if ( create_map ) { if (create_map) {
pa.createMapFiles() ; printAttributes.createMapFiles();
} }
// Tell the compiler to use our ICGASTconsumer // Tell the compiler to use our ICGASTconsumer
ICGASTConsumer *astConsumer = new ICGASTConsumer(ci, hsd, cs, pa); ICGASTConsumer* astConsumer = new ICGASTConsumer(compilerInstance, headerSearchDirs, commentSaver, printAttributes);
#if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 6)) #if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 6))
std::unique_ptr<clang::ASTConsumer> unique_ast(astConsumer) ; compilerInstance.setASTConsumer(std::move(std::unique_ptr<clang::ASTConsumer>(astConsumer)));
ci.setASTConsumer(std::move(unique_ast));
#else #else
ci.setASTConsumer(astConsumer); compilerInstance.setASTConsumer(astConsumer);
#endif #endif
ci.createASTContext(); compilerInstance.createASTContext();
ci.createSema(clang::TU_Prefix, NULL); compilerInstance.createSema(clang::TU_Prefix, NULL);
// Get the full path of the file to be read. // Get the full path of the file to be read
char * input_file_cp = strdup(input_file_names[0].c_str()) ; char buffer[input_file_names[0].size()];
char * input_file_file = basename(input_file_cp) ; strcpy(buffer, input_file_names[0].c_str());
char * input_file_dir = dirname(input_file_cp) ; std::string path(dirname(buffer));
char * input_file_full_path = NULL ; path += "/";
std::stringstream os ; strcpy(buffer, input_file_names[0].c_str());
os << input_file_dir << "/" << input_file_file ; path += basename(buffer);
input_file_full_path = almostRealPath( os.str().c_str() ) ; char* inputFilePath = almostRealPath(path);
//std::cout << input_file_full_path << std::endl ;
struct stat buffer ; struct stat dummy;
if ( stat ( input_file_full_path , &buffer) != 0 ) { if (stat(inputFilePath, &dummy)) {
std::cerr << "Could not open file " << input_file_full_path << std::endl ; std::cerr << "Could not open file " << inputFilePath << std::endl;
exit(-1) ; exit(-1);
} }
// Open up the input file and parse it. // Open up the input file and parse it
const clang::FileEntry *pFile = ci.getFileManager().getFile(input_file_full_path); const clang::FileEntry* fileEntry = compilerInstance.getFileManager().getFile(inputFilePath);
free(inputFilePath);
#if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5)) #if (LIBCLANG_MAJOR > 3) || ((LIBCLANG_MAJOR == 3) && (LIBCLANG_MINOR >= 5))
ci.getSourceManager().setMainFileID(ci.getSourceManager().createFileID(pFile, clang::SourceLocation(), clang::SrcMgr::C_User)); compilerInstance.getSourceManager().setMainFileID(compilerInstance.getSourceManager().createFileID(fileEntry, clang::SourceLocation(), clang::SrcMgr::C_User));
#else #else
ci.getSourceManager().createMainFileID(pFile); compilerInstance.getSourceManager().createMainFileID(fileEntry);
#endif #endif
ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(), &ci.getPreprocessor()); compilerInstance.getDiagnosticClient().BeginSourceFile(compilerInstance.getLangOpts(), &compilerInstance.getPreprocessor());
clang::ParseAST(ci.getSema()); clang::ParseAST(compilerInstance.getSema());
ci.getDiagnosticClient().EndSourceFile(); compilerInstance.getDiagnosticClient().EndSourceFile();
free(input_file_cp) ; if (!sim_services_flag) {
free(input_file_full_path) ; printAttributes.printIOMakefile();
if ( ! sim_services_flag ) {
pa.printIOMakefile() ;
} }
// Close the map files // Close the map files
pa.closeMapFiles() ; printAttributes.closeMapFiles();
// Print the list of headers that have the ICG:(No) comment // Print the list of headers that have the ICG:(No) comment
pa.printICGNoFiles() ; printAttributes.printICGNoFiles();
return 0; return 0;
} }