|
1 |
| -#include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 1 | +#include "llvm/ADT/StringMap.h" |
2 | 2 | #include "llvm/Support/CommandLine.h"
|
3 | 3 | #include "llvm/Support/Host.h"
|
4 | 4 | #include "clang/AST/ASTContext.h"
|
|
16 | 16 | #include "clang/Lex/Preprocessor.h"
|
17 | 17 | #include "clang/Parse/Parser.h"
|
18 | 18 | #include "clang/Parse/ParseAST.h"
|
19 |
| -#include <iostream> |
| 19 | + |
20 | 20 | using namespace llvm;
|
21 | 21 | using namespace clang;
|
22 | 22 | static cl::opt<std::string> FileName(cl::Positional, cl::desc("Input file"),
|
23 | 23 | cl::Required);
|
| 24 | + |
24 | 25 | int main(int argc, char **argv) {
|
25 | 26 | cl::ParseCommandLineOptions(argc, argv, "My simple front end\n");
|
26 | 27 | CompilerInstance CI;
|
27 | 28 | DiagnosticOptions diagnosticOptions;
|
28 |
| - CI.createDiagnostics(); |
29 |
| - // IntrusiveRefCntPtr<TargetOptions> PTO(new TargetOptions()); |
| 29 | + CI.createDiagnostics(nullptr, true); |
30 | 30 | auto PTO = std::make_shared<TargetOptions>(TargetOptions());
|
31 | 31 | PTO->Triple = sys::getDefaultTargetTriple();
|
| 32 | + { |
| 33 | + llvm::errs() << "targetTriple:" << sys::getDefaultTargetTriple() << '\n' |
| 34 | + << "cpu name: " << sys::getHostCPUName() << '\n' |
| 35 | + << "process triple: " << sys::getProcessTriple() << '\n' |
| 36 | + << "Big endian: " << sys::IsBigEndianHost << '\n'; |
| 37 | + StringMap<bool> strMap; |
| 38 | + bool succeed = sys::getHostCPUFeatures(strMap); |
| 39 | + if (succeed) { |
| 40 | + errs() << "features: "; |
| 41 | + for (auto &feature : strMap) { |
| 42 | + errs() << feature.getKey() << ": " << feature.getValue(); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
32 | 46 | TargetInfo *PTI = TargetInfo::CreateTargetInfo(CI.getDiagnostics(), PTO);
|
33 | 47 | CI.setTarget(PTI);
|
34 | 48 |
|
35 | 49 | CI.createFileManager();
|
36 |
| - CI.createSourceManager(CI.getFileManager()); |
| 50 | + FileManager &filemgr = CI.getFileManager(); |
| 51 | + CI.createSourceManager(filemgr); |
37 | 52 | CI.createPreprocessor(TranslationUnitKind::TU_Complete);
|
38 | 53 | CI.getPreprocessorOpts().UsePredefines = false;
|
39 | 54 | ASTConsumer *astConsumer = CreateASTPrinter(NULL, "");
|
40 | 55 | CI.setASTConsumer(astConsumer);
|
41 | 56 | CI.createASTContext();
|
42 | 57 | CI.createSema(TU_Complete, NULL);
|
| 58 | + { |
| 59 | + errs() << "hasdiagnostics: " << CI.hasDiagnostics() << '\n'; |
| 60 | + errs() << "hasinvocation: " << CI.hasInvocation() << '\n'; |
| 61 | + { |
| 62 | + auto &headerSearchOpts = CI.getHeaderSearchOpts(); |
| 63 | + errs() << "sysroot: " << headerSearchOpts.Sysroot << '\n'; |
| 64 | + errs() << "resourceDir: " << headerSearchOpts.ResourceDir << '\n'; |
| 65 | + errs() << "ModuleUserBuildPath: " << headerSearchOpts.ModuleUserBuildPath |
| 66 | + << '\n'; |
| 67 | + errs() << "header user entries:\t"; |
| 68 | + for (auto &entry : headerSearchOpts.UserEntries) { |
| 69 | + errs() << entry.Path << " "; |
| 70 | + } |
| 71 | + errs() << '\n'; |
| 72 | + errs() << "system header entries:\t"; |
| 73 | + for (auto &entry : headerSearchOpts.SystemHeaderPrefixes) { |
| 74 | + errs() << entry.Prefix << ": " << entry.IsSystemHeader << '\t'; |
| 75 | + } |
| 76 | + errs() << '\n'; |
| 77 | + for (auto &ignoredMacro : headerSearchOpts.ModulesIgnoreMacros) { |
| 78 | + errs() << ignoredMacro << '\n'; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
43 | 82 | FileEntry const *pFile = CI.getFileManager().getFile(FileName);
|
44 | 83 | if (!pFile) {
|
45 |
| - std::cerr << "File not found: " << FileName << std::endl; |
| 84 | + errs() << "File not found: " << FileName << '\n'; |
46 | 85 | return 1;
|
47 | 86 | }
|
| 87 | + { |
| 88 | + errs() << "name: " << pFile->getName() << '\n'; |
| 89 | + errs() << "dir: " << pFile->getDir()->getName() << '\n'; |
| 90 | + errs() << "size: " << pFile->getSize() << '\n'; |
| 91 | + errs() << "uid:" << pFile->getUID() << " uniqueID: " << pFile->getUID() |
| 92 | + << '\n'; |
| 93 | + } |
48 | 94 | SourceManager &sm = CI.getSourceManager();
|
49 |
| - sm.createFileID(pFile, SourceLocation(), SrcMgr::C_User); |
| 95 | + sm.setMainFileID(sm.createFileID(pFile, SourceLocation(), SrcMgr::C_User)); |
50 | 96 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
|
51 |
| - ParseAST(CI.getSema()); |
| 97 | + /// ParseAST(CI.getSema()); |
52 | 98 | // Print AST statistics
|
53 |
| - CI.getASTContext().PrintStats(); |
| 99 | + /// CI.getASTContext().PrintStats(); |
54 | 100 | CI.getASTContext().Idents.PrintStats();
|
55 | 101 | return 0;
|
56 | 102 | }
|
0 commit comments