Advertisement
Guest User

clang compiler invocation

a guest
Mar 3rd, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 KB | None | 0 0
  1.  
  2. #include "llvm/Option/Arg.h"
  3. #include "clang/Driver/DriverDiagnostic.h"
  4. #include "clang/Driver/Options.h"
  5. #include "clang/Basic/TargetOptions.h"
  6. #include "clang/Basic/TargetInfo.h"
  7. #include "clang/Frontend/CompilerInstance.h"
  8. #include "clang/Frontend/CompilerInvocation.h"
  9. #include "clang/Frontend/FrontendDiagnostic.h"
  10. #include "clang/Frontend/TextDiagnosticBuffer.h"
  11. #include "clang/Frontend/TextDiagnosticPrinter.h"
  12. #include "clang/Lex/Preprocessor.h"
  13. #include "clang/Parse/ParseAST.h"
  14. #include "clang/Rewrite/Core/Rewriter.h"
  15. #include "clang/Rewrite/Frontend/Rewriters.h"
  16. #include "clang/FrontendTool/Utils.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/LinkAllPasses.h"
  20. #include "llvm/Option/ArgList.h"
  21. #include "llvm/Option/OptTable.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include "llvm/Support/ManagedStatic.h"
  24. #include "llvm/Support/Signals.h"
  25. #include "llvm/Support/TargetSelect.h"
  26. #include "llvm/Support/Timer.h"
  27. #include "llvm/Support/raw_ostream.h"
  28.  
  29. using namespace clang;
  30. using namespace clang::vfs;
  31. using namespace llvm;
  32. using namespace llvm::opt;
  33.  
  34. static std::vector<const char*> win32_defaults = {
  35. "-triple",
  36. "i686-pc-windows-msvc",
  37. "-emit-obj",
  38. "-mrelax-all",
  39. "-disable-free",
  40. "-disable-llvm-verifier",
  41. "-mrelocation-model",
  42. "static",
  43. "-mdisable-fp-elim",
  44. "-std=c++14",
  45. "-masm-verbose",
  46. "-mconstructor-aliases",
  47. "-target-cpu",
  48. "pentium4",
  49. "-D_MT",
  50. "--dependent-lib=libcmt",
  51. "--dependent-lib=oldnames",
  52. "-fms-extensions",
  53. "-fms-compatibility",
  54. "-fms-compatibility-version=19.0.24215",
  55. "-fdiagnostics-format",
  56. "msvc",
  57. "-D_HAS_EXCEPTIONS=1",
  58. "-fno-dwarf-directory-asm",
  59. "-ferror-limit",
  60. "19",
  61. "-fmessage-length",
  62. "80",
  63. "-mstackrealign",
  64. "-mstack-alignment=16",
  65. "-target-feature",
  66. "+sse4.2",
  67. "-fobjc-runtime=gcc",
  68. "-fdiagnostics-show-option",
  69. "-fcolor-diagnostics",
  70. "-fdelayed-template-parsing",
  71. "-fcxx-exceptions",
  72. "-fexceptions",
  73. "-internal-isystem",
  74. "E:\\Microsoft Visual Studio 14.0\\VC\\include",
  75. "-internal-isystem",
  76. "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.10240.0\\ucrt",
  77. "-internal-isystem",
  78. "E:\\work\\git\\scripta\\trunk\\include",
  79. "-internal-isystem",
  80. "F:\\Libraries\\eigen\\Eigen"
  81. };
  82.  
  83. #if defined(WIN32)
  84. #include <windows.h>
  85.  
  86. BOOL APIENTRY  DllMain(HMODULE hModule, DWORD Reason, LPVOID pReserved)
  87. {
  88.     switch (Reason)
  89.     {
  90.     case DLL_PROCESS_ATTACH:
  91.     case DLL_THREAD_ATTACH:
  92.     case DLL_THREAD_DETACH:
  93.     case DLL_PROCESS_DETACH:
  94.         break;
  95.     }
  96.     return TRUE;
  97. }
  98. #endif
  99.  
  100.  
  101.  
  102. extern "C"
  103. {
  104.     __declspec (dllexport) int test()
  105.     {
  106.  
  107.         std::vector<const char*> compiler_args = win32_defaults;
  108.  
  109.         compiler_args.push_back("-o");
  110.         compiler_args.push_back("out");
  111.         compiler_args.push_back("-x");
  112.         compiler_args.push_back("c++");
  113.         compiler_args.push_back("Editor.cpp");
  114.         compiler_args.push_back("-v");
  115.  
  116.         LLVMInitializeX86TargetInfo();
  117.         LLVMInitializeX86Target();
  118.         LLVMInitializeX86TargetMC();
  119.         LLVMInitializeX86AsmPrinter();
  120.         LLVMInitializeX86AsmParser();
  121.  
  122.         CompilerInstance m_compilerParseInstance;
  123.  
  124.         //m_compilerParseInstance.createFileManager();
  125.  
  126.         DiagnosticIDs m_diagIDsParse;
  127.         DiagnosticOptions m_diagOptsParse;
  128.         clang::TextDiagnosticBuffer m_diagsBufferParse;
  129.  
  130.         new clang::DiagnosticsEngine(&m_diagIDsParse, &m_diagOptsParse, &m_diagsBufferParse, false);
  131.  
  132.         m_compilerParseInstance.createDiagnostics(&m_diagsBufferParse, false);
  133.  
  134.         LangOptions &lo = m_compilerParseInstance.getLangOpts();
  135.         lo.CPlusPlus = 1;
  136.  
  137.         //  m_compilerParseInstance.createSourceManager(m_compilerParseInstance.getFileManager());
  138.  
  139.         bool Success = CompilerInvocation::CreateFromArgs(m_compilerParseInstance.getInvocation(),
  140.             &compiler_args.front(), &compiler_args.back(), m_compilerParseInstance.getDiagnostics());
  141.  
  142.         // Execute the frontend actions.
  143.         Success = ExecuteCompilerInvocation(&m_compilerParseInstance);
  144.  
  145.         if (!Success) printf("WE FAILED TO RUN THE COMPILER\n");
  146.  
  147.         clang::SourceManager& source_manager = m_compilerParseInstance.getSourceManager();
  148.  
  149.         printf("Number of errors: %d\n", m_diagsBufferParse.getNumErrors());
  150.         printf("Number of warnings: %d\n", m_diagsBufferParse.getNumWarnings());
  151.  
  152.         for (clang::TextDiagnosticBuffer::const_iterator it = m_diagsBufferParse.err_begin(); it != m_diagsBufferParse.err_end(); ++it)
  153.         {
  154.             clang::PresumedLoc presumed_location = source_manager.getPresumedLoc(it->first);
  155.  
  156.             if (it->first.isValid())
  157.             {
  158.                 clang::PresumedLoc presumed_location = source_manager.getPresumedLoc(it->first);
  159.  
  160.                 size_t fileSize = strlen(presumed_location.getFilename());
  161.                 size_t msgSize = it->second.size();
  162.  
  163.                 printf("Error message: %s\n", it->second.c_str());
  164.             }
  165.             else // compiler error (not specific to any file)
  166.             {
  167.                 size_t fileSize = 14;
  168.                 size_t msgSize = it->second.size();
  169.  
  170.                 printf("Error message: %s\n", it->second.c_str());
  171.             }
  172.         }
  173.  
  174.         getchar();
  175.  
  176.         return 0;
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement