Computer Laboratory

Tool.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2013 Jonathan Anderson
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7  * ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include "AST.h"
32 #include "Debug.h"
33 
34 #include "llvm/Support/CommandLine.h"
35 
36 #include "clang/Frontend/CompilerInstance.h"
37 #include "clang/Frontend/FrontendActions.h"
38 #include "clang/Tooling/CompilationDatabase.h"
39 #include "clang/Tooling/Tooling.h"
40 
41 #include "llvm/Support/PrettyStackTrace.h"
42 
43 #include <google/protobuf/text_format.h>
44 
45 using namespace clang::tooling;
46 using namespace llvm;
47 using namespace tesla;
48 
49 using std::string;
50 
51 
52 cl::opt<string> OutputFile(
53  "o",
54  cl::desc("<output file>"),
55  cl::Required);
56 
57 cl::list<string> SourcePaths(
58  cl::Positional,
59  cl::desc("<source0> [... <sourceN>]"),
60  cl::OneOrMore);
61 
62 
63 int main(int argc, const char **argv) {
64  llvm::PrettyStackTraceProgram X(argc, argv);
65 
66  // Add a preprocessor definition to indicate we're doing TESLA parsing.
67  std::vector<const char*> args(argv, argv + argc);
68  args.push_back("-D");
69  args.push_back("__TESLA_ANALYSER__");
70 
71  // Change argc and argv to refer to the vector's memory.
72  // The CompilationDatabase will modify these, so we shouldn't pass in
73  // args.data() directly.
74  argc = (int) args.size();
75  assert(((size_t) argc) == args.size()); // check for overflow
76 
77  argv = args.data();
78 
79  OwningPtr<CompilationDatabase> Compilations(
80  FixedCompilationDatabase::loadFromCommandLine(argc, argv));
81 
82  if (!Compilations)
83  panic(
84  "Need compilation options, e.g. tesla-analyser foo.c -- -I ../include");
85 
86  cl::ParseCommandLineOptions(argc, argv);
87 
88  OwningPtr<TeslaActionFactory> Factory(new TeslaActionFactory(OutputFile));
89 
90  ClangTool Tool(*Compilations, SourcePaths);
91  return Tool.run(Factory.get());
92 }
93