blob: 364f3281572bdd860ad17162e3ca2d9ac991e634 [file] [log] [blame]
Daniel Dunbar63c4da92009-03-02 19:59:07 +00001//===--- Compilation.cpp - Compilation Task Implementation --------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Driver/Compilation.h"
Daniel Dunbar9d625e12009-03-16 06:42:30 +000011
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +000012#include "clang/Driver/Action.h"
Daniel Dunbar9d625e12009-03-16 06:42:30 +000013#include "clang/Driver/ArgList.h"
14#include "clang/Driver/ToolChain.h"
15
Daniel Dunbar1b265972009-03-18 06:49:39 +000016#include "llvm/Support/raw_ostream.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000017using namespace clang::driver;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000018
Daniel Dunbar9d625e12009-03-16 06:42:30 +000019Compilation::Compilation(ToolChain &_DefaultToolChain,
20 ArgList *_Args)
21 : DefaultToolChain(_DefaultToolChain), Args(_Args) {
Daniel Dunbar63c4da92009-03-02 19:59:07 +000022}
23
Daniel Dunbar9d625e12009-03-16 06:42:30 +000024Compilation::~Compilation() {
25 delete Args;
26
27 // Free any derived arg lists.
28 for (llvm::DenseMap<const ToolChain*, ArgList*>::iterator
29 it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it) {
30 ArgList *A = it->second;
31 if (A != Args)
32 delete Args;
33 }
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +000034
35 // Free the actions, if built.
36 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
37 it != ie; ++it)
38 delete *it;
Daniel Dunbar9d625e12009-03-16 06:42:30 +000039}
40
41const ArgList &Compilation::getArgsForToolChain(const ToolChain *TC) {
42 if (!TC)
43 TC = &DefaultToolChain;
44
Daniel Dunbare5640282009-03-18 05:58:45 +000045 ArgList *&Entry = TCArgs[TC];
46 if (!Entry)
47 Entry = TC->TranslateArgs(*Args);
Daniel Dunbar9d625e12009-03-16 06:42:30 +000048
Daniel Dunbare5640282009-03-18 05:58:45 +000049 return *Entry;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000050}
51
Daniel Dunbar1b265972009-03-18 06:49:39 +000052void Compilation::PrintJob(llvm::raw_ostream &OS, const Job *J,
53 const char *Terminator) const {
54 if (const Command *C = dyn_cast<Command>(J)) {
55 OS << " \"" << C->getExecutable() << '"';
56 for (ArgStringList::const_iterator it = C->getArguments().begin(),
57 ie = C->getArguments().end(); it != ie; ++it)
58 OS << " \"" << *it << '"';
59 OS << Terminator;
60 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(J)) {
61 for (PipedJob::const_iterator
62 it = PJ->begin(), ie = PJ->end(); it != ie; ++it)
63 PrintJob(OS, *it, (it + 1 != PJ->end()) ? " |\n" : "\n");
64 } else {
65 const JobList *Jobs = cast<JobList>(J);
66 for (JobList::const_iterator
67 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
68 PrintJob(OS, *it, Terminator);
69 }
70}
71
Daniel Dunbar63c4da92009-03-02 19:59:07 +000072int Compilation::Execute() const {
Daniel Dunbar1b265972009-03-18 06:49:39 +000073 // Just print if -### was present.
74 if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
75 PrintJob(llvm::errs(), &Jobs, "\n");
76 return 0;
77 }
78
Daniel Dunbar63c4da92009-03-02 19:59:07 +000079 return 0;
80}