]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - unittests/DriverTests/DriverTest.h
Vendor import of lld trunk r233088:
[FreeBSD/FreeBSD.git] / unittests / DriverTests / DriverTest.h
1 //===- lld/unittest/DriverTest.h ------------------------------------------===//
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 "lld/Driver/Driver.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
13 #include <stdarg.h>
14
15 namespace {
16
17 using namespace llvm;
18 using namespace lld;
19
20 template<typename D, typename T>
21 class ParserTest : public testing::Test {
22 protected:
23
24   virtual const LinkingContext *linkingContext() = 0;
25
26   std::string &errorMessage() { return  _errorMessage; }
27
28   // Convenience method for getting number of input files.
29   int inputFileCount() {
30     return linkingContext()->getNodes().size();
31   }
32
33   // Convenience method for getting i'th input files name.
34   std::string inputFile(int index) {
35     Node &node = *linkingContext()->getNodes()[index];
36     if (node.kind() == Node::Kind::File)
37       return cast<FileNode>(&node)->getFile()->path();
38     llvm_unreachable("not handling other types of input files");
39   }
40
41   // For unit tests to call driver with various command lines.
42   bool parse(const char *args, ...) {
43     // Construct command line options from varargs.
44     std::vector<const char *> vec;
45     vec.push_back(args);
46     va_list ap;
47     va_start(ap, args);
48     while (const char *arg = va_arg(ap, const char *))
49       vec.push_back(arg);
50     va_end(ap);
51
52     // Call the parser.
53     raw_string_ostream os(_errorMessage);
54     return D::parse(vec.size(), &vec[0], _ctx, os);
55   }
56
57   T _ctx;
58   std::string _errorMessage;
59 };
60
61 }