LLVM API Documentation

Tool.cpp

Go to the documentation of this file.
00001 //===--- Tool.cpp - The LLVM Compiler Driver --------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open
00006 // Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  Tool base class - implementation details.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/CompilerDriver/BuiltinOptions.h"
00015 #include "llvm/CompilerDriver/Tool.h"
00016 
00017 #include "llvm/ADT/StringExtras.h"
00018 #include "llvm/System/Path.h"
00019 
00020 using namespace llvm;
00021 using namespace llvmc;
00022 
00023 // SplitString is used by derived Tool classes.
00024 typedef void (*SplitStringFunPtr)(const std::string&,
00025                                   std::vector<std::string>&, const char*);
00026 SplitStringFunPtr ForceLinkageSplitString = &llvm::SplitString;
00027 
00028 namespace {
00029   sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
00030                          const std::string& Suffix) {
00031     sys::Path Out;
00032 
00033     // Make sure we don't end up with path names like '/file.o' if the
00034     // TempDir is empty.
00035     if (TempDir.empty()) {
00036       Out.set(BaseName);
00037     }
00038     else {
00039       Out = TempDir;
00040       Out.appendComponent(BaseName);
00041     }
00042     Out.appendSuffix(Suffix);
00043     // NOTE: makeUnique always *creates* a unique temporary file,
00044     // which is good, since there will be no races. However, some
00045     // tools do not like it when the output file already exists, so
00046     // they need to be placated with -f or something like that.
00047     Out.makeUnique(true, NULL);
00048     return Out;
00049   }
00050 }
00051 
00052 sys::Path Tool::OutFilename(const sys::Path& In,
00053                             const sys::Path& TempDir,
00054                             bool StopCompilation,
00055                             const char* OutputSuffix) const {
00056   sys::Path Out;
00057 
00058   if (StopCompilation) {
00059     if (!OutputFilename.empty()) {
00060       Out.set(OutputFilename);
00061     }
00062     else if (IsJoin()) {
00063       Out.set("a");
00064       Out.appendSuffix(OutputSuffix);
00065     }
00066     else {
00067       Out.set(In.getBasename());
00068       Out.appendSuffix(OutputSuffix);
00069     }
00070   }
00071   else {
00072     if (IsJoin())
00073       Out = MakeTempFile(TempDir, "tmp", OutputSuffix);
00074     else
00075       Out = MakeTempFile(TempDir, In.getBasename(), OutputSuffix);
00076   }
00077   return Out;
00078 }