Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | 0a813a3 | 2012-08-03 16:52:40 -0700 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Stephen Hines | 8b5c5c6 | 2014-06-06 18:03:18 -0700 | [diff] [blame] | 17 | #include "clang/Basic/DiagnosticOptions.h" |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 18 | #include "clang/Driver/DriverDiagnostic.h" |
Stephen Hines | a1f95ee | 2013-08-09 01:26:08 -0700 | [diff] [blame] | 19 | #include "clang/Driver/Options.h" |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 20 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Stephen Hines | 8f4d972 | 2011-12-05 14:14:42 -0800 | [diff] [blame] | 21 | #include "clang/Frontend/Utils.h" |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 22 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 24 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 25 | |
Stephen Hines | a1f95ee | 2013-08-09 01:26:08 -0700 | [diff] [blame] | 26 | #include "llvm/Option/OptTable.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/ManagedStatic.h" |
| 29 | #include "llvm/Support/MemoryBuffer.h" |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 30 | #include "llvm/Support/Path.h" |
Stephen Hines | ba7c6dc | 2011-09-07 19:57:04 -0700 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
mkopec1 | c460b37 | 2012-01-09 11:21:50 -0500 | [diff] [blame] | 32 | #include "llvm/Target/TargetMachine.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 33 | |
Stephen Hines | 8b5c5c6 | 2014-06-06 18:03:18 -0700 | [diff] [blame] | 34 | #include "rs_cc_options.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 35 | #include "slang.h" |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 36 | #include "slang_assert.h" |
Stephen Hines | 8f4d972 | 2011-12-05 14:14:42 -0800 | [diff] [blame] | 37 | #include "slang_diagnostic_buffer.h" |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 38 | #include "slang_rs.h" |
| 39 | #include "slang_rs_reflect_utils.h" |
| 40 | |
Stephen Hines | 8b5c5c6 | 2014-06-06 18:03:18 -0700 | [diff] [blame] | 41 | #include <list> |
| 42 | #include <set> |
| 43 | #include <string> |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 44 | |
| 45 | // SaveStringInSet, ExpandArgsFromBuf and ExpandArgv are all copied from |
| 46 | // $(CLANG_ROOT)/tools/driver/driver.cpp for processing argc/argv passed in |
| 47 | // main(). |
| 48 | static inline const char *SaveStringInSet(std::set<std::string> &SavedStrings, |
| 49 | llvm::StringRef S) { |
| 50 | return SavedStrings.insert(S).first->c_str(); |
| 51 | } |
| 52 | static void ExpandArgsFromBuf(const char *Arg, |
| 53 | llvm::SmallVectorImpl<const char*> &ArgVector, |
| 54 | std::set<std::string> &SavedStrings); |
| 55 | static void ExpandArgv(int argc, const char **argv, |
| 56 | llvm::SmallVectorImpl<const char*> &ArgVector, |
| 57 | std::set<std::string> &SavedStrings); |
| 58 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 59 | static const char *DetermineOutputFile(const std::string &OutputDir, |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 60 | const std::string &PathSuffix, |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 61 | const char *InputFile, |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 62 | slang::Slang::OutputType OutputType, |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 63 | std::set<std::string> &SavedStrings) { |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 64 | if (OutputType == slang::Slang::OT_Nothing) |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 65 | return "/dev/null"; |
| 66 | |
| 67 | std::string OutputFile(OutputDir); |
| 68 | |
Stephen Hines | 7f5704e | 2014-06-10 18:06:50 -0700 | [diff] [blame] | 69 | // Append '/' to Opts.mBitcodeOutputDir if not presents |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 70 | if (!OutputFile.empty() && |
Raphael | 8d5a2f6 | 2011-02-08 00:15:05 -0800 | [diff] [blame] | 71 | (OutputFile[OutputFile.size() - 1]) != OS_PATH_SEPARATOR) |
| 72 | OutputFile.append(1, OS_PATH_SEPARATOR); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 73 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 74 | if (!PathSuffix.empty()) { |
| 75 | OutputFile.append(PathSuffix); |
| 76 | OutputFile.append(1, OS_PATH_SEPARATOR); |
| 77 | } |
| 78 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 79 | if (OutputType == slang::Slang::OT_Dependency) { |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 80 | // The build system wants the .d file name stem to be exactly the same as |
| 81 | // the source .rs file, instead of the .bc file. |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 82 | OutputFile.append(slang::RSSlangReflectUtils::GetFileNameStem(InputFile)); |
| 83 | } else { |
| 84 | OutputFile.append( |
| 85 | slang::RSSlangReflectUtils::BCFileNameFromRSFileName(InputFile)); |
| 86 | } |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 87 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 88 | switch (OutputType) { |
| 89 | case slang::Slang::OT_Dependency: { |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 90 | OutputFile.append(".d"); |
| 91 | break; |
| 92 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 93 | case slang::Slang::OT_Assembly: { |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 94 | OutputFile.append(".S"); |
| 95 | break; |
| 96 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 97 | case slang::Slang::OT_LLVMAssembly: { |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 98 | OutputFile.append(".ll"); |
| 99 | break; |
| 100 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 101 | case slang::Slang::OT_Object: { |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 102 | OutputFile.append(".o"); |
| 103 | break; |
| 104 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 105 | case slang::Slang::OT_Bitcode: { |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 106 | OutputFile.append(".bc"); |
| 107 | break; |
| 108 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 109 | case slang::Slang::OT_Nothing: |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 110 | default: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 111 | slangAssert(false && "Invalid output type!"); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | return SaveStringInSet(SavedStrings, OutputFile); |
| 116 | } |
| 117 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 118 | typedef std::list<std::pair<const char*, const char*> > NamePairList; |
| 119 | |
| 120 | /* |
| 121 | * Compile the Inputs. |
| 122 | * |
| 123 | * Returns 0 on success and nonzero on failure. |
| 124 | * |
| 125 | * IOFiles - list of (foo.rs, foo.bc) pairs of input/output files. |
| 126 | * IOFiles32 - list of input/output pairs for 32-bit compilation. |
| 127 | * Inputs - input filenames. |
| 128 | * Opts - options controlling compilation. |
| 129 | * DiagEngine - Clang diagnostic engine (for creating diagnostics). |
| 130 | * DiagClient - Slang diagnostic consumer (collects and displays diagnostics). |
| 131 | * SavedStrings - expanded strings copied from argv source input files. |
| 132 | * |
| 133 | * We populate IOFiles dynamically while working through the list of Inputs. |
| 134 | * On any 64-bit compilation, we pass back in the 32-bit pairs of files as |
| 135 | * IOFiles32. This allows the 64-bit compiler to later bundle up both the |
| 136 | * 32-bit and 64-bit bitcode outputs to be included in the final reflected |
| 137 | * source code that is emitted. |
| 138 | */ |
| 139 | static int compileFiles(NamePairList *IOFiles, NamePairList *IOFiles32, |
| 140 | const llvm::SmallVector<const char*, 16> &Inputs, slang::RSCCOptions &Opts, |
| 141 | clang::DiagnosticsEngine *DiagEngine, slang::DiagnosticBuffer *DiagClient, |
| 142 | std::set<std::string> *SavedStrings) { |
| 143 | NamePairList DepFiles; |
| 144 | std::string PathSuffix = ""; |
| 145 | |
| 146 | // In our mixed 32/64-bit path, we need to suffix our files differently for |
| 147 | // both 32-bit and 64-bit versions. |
| 148 | if (Opts.mEmit3264) { |
| 149 | if (Opts.mBitWidth == 64) { |
| 150 | PathSuffix = "bc64"; |
| 151 | } else { |
| 152 | PathSuffix = "bc32"; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | for (int i = 0, e = Inputs.size(); i != e; i++) { |
| 157 | const char *InputFile = Inputs[i]; |
| 158 | |
| 159 | const char *BCOutputFile = DetermineOutputFile(Opts.mBitcodeOutputDir, |
| 160 | PathSuffix, InputFile, |
| 161 | slang::Slang::OT_Bitcode, |
| 162 | *SavedStrings); |
| 163 | const char *OutputFile = BCOutputFile; |
| 164 | |
| 165 | if (Opts.mEmitDependency) { |
| 166 | // The dependency file is always emitted without a PathSuffix. |
| 167 | // Collisions between 32-bit and 64-bit files don't make a difference, |
| 168 | // because they share the same sources/dependencies. |
| 169 | const char *DepOutputFile = |
| 170 | DetermineOutputFile(Opts.mDependencyOutputDir, "", InputFile, |
| 171 | slang::Slang::OT_Dependency, *SavedStrings); |
| 172 | if (Opts.mOutputType == slang::Slang::OT_Dependency) { |
| 173 | OutputFile = DepOutputFile; |
| 174 | } |
| 175 | |
| 176 | DepFiles.push_back(std::make_pair(BCOutputFile, DepOutputFile)); |
| 177 | } |
| 178 | |
| 179 | IOFiles->push_back(std::make_pair(InputFile, OutputFile)); |
| 180 | } |
| 181 | |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 182 | std::unique_ptr<slang::SlangRS> Compiler(new slang::SlangRS()); |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 183 | Compiler->init(Opts.mBitWidth, DiagEngine, DiagClient); |
| 184 | int CompileFailed = !Compiler->compile(*IOFiles, *IOFiles32, DepFiles, Opts); |
| 185 | Compiler->reset(); |
| 186 | return CompileFailed; |
| 187 | } |
| 188 | |
Stephen Hines | ba7c6dc | 2011-09-07 19:57:04 -0700 | [diff] [blame] | 189 | #define str(s) #s |
| 190 | #define wrap_str(s) str(s) |
| 191 | static void llvm_rs_cc_VersionPrinter() { |
| 192 | llvm::raw_ostream &OS = llvm::outs(); |
| 193 | OS << "llvm-rs-cc: Renderscript compiler\n" |
| 194 | << " (http://developer.android.com/guide/topics/renderscript)\n" |
| 195 | << " based on LLVM (http://llvm.org):\n"; |
| 196 | OS << " Built " << __DATE__ << " (" << __TIME__ ").\n"; |
| 197 | OS << " Target APIs: " << SLANG_MINIMUM_TARGET_API << " - " |
| 198 | << SLANG_MAXIMUM_TARGET_API; |
| 199 | OS << "\n Build type: " << wrap_str(TARGET_BUILD_VARIANT); |
| 200 | #ifndef __DISABLE_ASSERTS |
| 201 | OS << " with assertions"; |
| 202 | #endif |
| 203 | OS << ".\n"; |
Stephen Hines | ba7c6dc | 2011-09-07 19:57:04 -0700 | [diff] [blame] | 204 | } |
Stephen Hines | 8f4d972 | 2011-12-05 14:14:42 -0800 | [diff] [blame] | 205 | #undef wrap_str |
| 206 | #undef str |
Stephen Hines | ba7c6dc | 2011-09-07 19:57:04 -0700 | [diff] [blame] | 207 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 208 | int main(int argc, const char **argv) { |
| 209 | std::set<std::string> SavedStrings; |
| 210 | llvm::SmallVector<const char*, 256> ArgVector; |
Stephen Hines | 8b5c5c6 | 2014-06-06 18:03:18 -0700 | [diff] [blame] | 211 | slang::RSCCOptions Opts; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 212 | llvm::SmallVector<const char*, 16> Inputs; |
| 213 | std::string Argv0; |
| 214 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 215 | llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 216 | |
| 217 | ExpandArgv(argc, argv, ArgVector, SavedStrings); |
| 218 | |
| 219 | // Argv0 |
Logan Chien | 6f4e0a9 | 2011-03-10 01:10:25 +0800 | [diff] [blame] | 220 | Argv0 = llvm::sys::path::stem(ArgVector[0]); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 221 | |
| 222 | // Setup diagnostic engine |
Stephen Hines | 8f4d972 | 2011-12-05 14:14:42 -0800 | [diff] [blame] | 223 | slang::DiagnosticBuffer *DiagClient = new slang::DiagnosticBuffer(); |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 224 | |
| 225 | llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagIDs( |
| 226 | new clang::DiagnosticIDs()); |
| 227 | |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 228 | llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts( |
| 229 | new clang::DiagnosticOptions()); |
| 230 | clang::DiagnosticsEngine DiagEngine(DiagIDs, &*DiagOpts, DiagClient, true); |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 231 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 232 | slang::Slang::GlobalInitialization(); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 233 | |
Stephen Hines | 8b5c5c6 | 2014-06-06 18:03:18 -0700 | [diff] [blame] | 234 | slang::ParseArguments(ArgVector, Inputs, Opts, DiagEngine); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 235 | |
| 236 | // Exits when there's any error occurred during parsing the arguments |
Stephen Hines | 8f4d972 | 2011-12-05 14:14:42 -0800 | [diff] [blame] | 237 | if (DiagEngine.hasErrorOccurred()) { |
| 238 | llvm::errs() << DiagClient->str(); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 239 | return 1; |
Stephen Hines | 8f4d972 | 2011-12-05 14:14:42 -0800 | [diff] [blame] | 240 | } |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 241 | |
| 242 | if (Opts.mShowHelp) { |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 243 | std::unique_ptr<llvm::opt::OptTable> OptTbl(slang::createRSCCOptTable()); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 244 | OptTbl->PrintHelp(llvm::outs(), Argv0.c_str(), |
Stephen Hines | b7d1269 | 2011-09-02 18:16:19 -0700 | [diff] [blame] | 245 | "Renderscript source compiler"); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | if (Opts.mShowVersion) { |
Stephen Hines | ba7c6dc | 2011-09-07 19:57:04 -0700 | [diff] [blame] | 250 | llvm_rs_cc_VersionPrinter(); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | // No input file |
| 255 | if (Inputs.empty()) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 256 | DiagEngine.Report(clang::diag::err_drv_no_input_files); |
Stephen Hines | 8f4d972 | 2011-12-05 14:14:42 -0800 | [diff] [blame] | 257 | llvm::errs() << DiagClient->str(); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 258 | return 1; |
| 259 | } |
| 260 | |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 261 | // Prepare input data for RS compiler. |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 262 | NamePairList IOFiles64; |
| 263 | NamePairList IOFiles32; |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 264 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 265 | int CompileFailed = compileFiles(&IOFiles32, &IOFiles32, Inputs, Opts, |
| 266 | &DiagEngine, DiagClient, &SavedStrings); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 267 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 268 | // Handle the 64-bit case too! |
| 269 | if (Opts.mEmit3264 && !CompileFailed) { |
| 270 | Opts.mBitWidth = 64; |
| 271 | CompileFailed = compileFiles(&IOFiles64, &IOFiles32, Inputs, Opts, |
| 272 | &DiagEngine, DiagClient, &SavedStrings); |
Zonr Chang | cf6af6a | 2010-10-12 12:38:51 +0800 | [diff] [blame] | 273 | } |
| 274 | |
Stephen Hines | d7f0ea2 | 2011-02-22 17:45:19 -0800 | [diff] [blame] | 275 | return CompileFailed; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /////////////////////////////////////////////////////////////////////////////// |
| 279 | |
| 280 | // ExpandArgsFromBuf - |
| 281 | static void ExpandArgsFromBuf(const char *Arg, |
| 282 | llvm::SmallVectorImpl<const char*> &ArgVector, |
| 283 | std::set<std::string> &SavedStrings) { |
| 284 | const char *FName = Arg + 1; |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 285 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr = |
| 286 | llvm::MemoryBuffer::getFile(FName); |
| 287 | if (MBOrErr.getError()) { |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 288 | // Unable to open the file |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 289 | ArgVector.push_back(SaveStringInSet(SavedStrings, Arg)); |
| 290 | return; |
| 291 | } |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 292 | std::unique_ptr<llvm::MemoryBuffer> MemBuf = std::move(MBOrErr.get()); |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 293 | |
| 294 | const char *Buf = MemBuf->getBufferStart(); |
| 295 | char InQuote = ' '; |
| 296 | std::string CurArg; |
| 297 | |
| 298 | for (const char *P = Buf; ; ++P) { |
| 299 | if (*P == '\0' || (isspace(*P) && InQuote == ' ')) { |
| 300 | if (!CurArg.empty()) { |
| 301 | if (CurArg[0] != '@') { |
| 302 | ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg)); |
| 303 | } else { |
| 304 | ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings); |
| 305 | } |
| 306 | |
| 307 | CurArg = ""; |
| 308 | } |
| 309 | if (*P == '\0') |
| 310 | break; |
| 311 | else |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | if (isspace(*P)) { |
| 316 | if (InQuote != ' ') |
| 317 | CurArg.push_back(*P); |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | if (*P == '"' || *P == '\'') { |
| 322 | if (InQuote == *P) |
| 323 | InQuote = ' '; |
| 324 | else if (InQuote == ' ') |
| 325 | InQuote = *P; |
| 326 | else |
| 327 | CurArg.push_back(*P); |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | if (*P == '\\') { |
| 332 | ++P; |
| 333 | if (*P != '\0') |
| 334 | CurArg.push_back(*P); |
| 335 | continue; |
| 336 | } |
| 337 | CurArg.push_back(*P); |
| 338 | } |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // ExpandArgsFromBuf - |
| 342 | static void ExpandArgv(int argc, const char **argv, |
| 343 | llvm::SmallVectorImpl<const char*> &ArgVector, |
| 344 | std::set<std::string> &SavedStrings) { |
| 345 | for (int i = 0; i < argc; ++i) { |
| 346 | const char *Arg = argv[i]; |
| 347 | if (Arg[0] != '@') { |
| 348 | ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg))); |
| 349 | continue; |
| 350 | } |
| 351 | |
| 352 | ExpandArgsFromBuf(Arg, ArgVector, SavedStrings); |
| 353 | } |
| 354 | } |