Add a ParseOptions function to handle common steps (#325)

Signed-off-by: Kévin Petit <kpet@free.fr>
diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp
index d4dc8ce..2ce13cd 100644
--- a/lib/Compiler.cpp
+++ b/lib/Compiler.cpp
@@ -670,10 +670,8 @@
 
   return 0;
 }
-} // namespace
 
-namespace clspv {
-int Compile(const int argc, const char *const argv[]) {
+int ParseOptions(const int argc, const char *const argv[]) {
   // We need to change how one of the called passes works by spoofing
   // ParseCommandLineOptions with the specific option.
   const int llvmArgc = 2;
@@ -682,10 +680,27 @@
       "-simplifycfg-sink-common=false",
   };
 
+  llvm::cl::ResetAllOptionOccurrences();
   llvm::cl::ParseCommandLineOptions(llvmArgc, llvmArgv);
-
   llvm::cl::ParseCommandLineOptions(argc, argv);
 
+  if (clspv::Option::ConstantArgsInUniformBuffer() &&
+      !clspv::Option::InlineEntryPoints()) {
+    llvm::errs() << "clspv restriction: -constant-args-ubo requires "
+                    "-inline-entry-points\n";
+    return -1;
+  }
+
+  return 0;
+}
+} // namespace
+
+namespace clspv {
+int Compile(const int argc, const char *const argv[]) {
+
+  if (auto error = ParseOptions(argc, argv))
+    return error;
+
   llvm::SmallVector<std::pair<unsigned, std::string>, 8> SamplerMapEntries;
   if (auto error = ParseSamplerMap("", &SamplerMapEntries))
     return error;
@@ -734,13 +749,6 @@
     return -1;
   }
 
-  if (clspv::Option::ConstantArgsInUniformBuffer() &&
-      !clspv::Option::InlineEntryPoints()) {
-    llvm::errs() << "clspv restriction: -constant-args-ubo requires "
-                    "-inline-entry-points\n";
-    return -1;
-  }
-
   // Don't run the passes or produce any output in verify mode.
   // Clang doesn't always produce a valid module.
   if (verify) {
@@ -816,16 +824,6 @@
                             const std::string &options,
                             std::vector<uint32_t> *output_binary,
                             std::vector<clspv::version0::DescriptorMapEntry> *descriptor_map_entries) {
-  // We need to change how one of the called passes works by spoofing
-  // ParseCommandLineOptions with the specific option.
-  const int llvmArgc = 2;
-  const char *llvmArgv[llvmArgc] = {
-      "clspv",
-      "-simplifycfg-sink-common=false",
-  };
-
-  llvm::cl::ResetAllOptionOccurrences();
-  llvm::cl::ParseCommandLineOptions(llvmArgc, llvmArgv);
 
   llvm::SmallVector<const char *, 20> argv;
   llvm::BumpPtrAllocator A;
@@ -833,7 +831,9 @@
   argv.push_back(Saver.save("clspv").data());
   llvm::cl::TokenizeGNUCommandLine(options, Saver, argv);
   int argc = static_cast<int>(argv.size());
-  llvm::cl::ParseCommandLineOptions(argc, &argv[0]);
+
+  if (auto error = ParseOptions(argc, &argv[0]))
+    return error;
 
   llvm::SmallVector<std::pair<unsigned, std::string>, 8> SamplerMapEntries;
   if (auto error = ParseSamplerMap(sampler_map, &SamplerMapEntries))
@@ -875,13 +875,6 @@
     return -1;
   }
 
-  if (clspv::Option::ConstantArgsInUniformBuffer() &&
-      !clspv::Option::InlineEntryPoints()) {
-    llvm::errs() << "clspv restriction: -constant-arg-ubo requires "
-                    "-inline-entry-points\n";
-    return -1;
-  }
-
   llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry();
   llvm::initializeCore(Registry);
   llvm::initializeScalarOpts(Registry);