Computer Laboratory

Visitor.cpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2013 Jonathan Anderson
4  * All rights reserved.
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "Names.h"
33 #include "Parser.h"
34 #include "Visitor.h"
35 
36 #include <clang/AST/ASTContext.h>
37 
38 using namespace clang;
39 using std::string;
40 
41 
42 namespace tesla {
43 
44 template<class T>
45 void ReportError(ASTContext& Ctx, StringRef Message, T *Subject) {
46  DiagnosticsEngine& Diag = Ctx.getDiagnostics();
47  int DiagID = Diag.getCustomDiagID(DiagnosticsEngine::Error,
48  ("TESLA: " + Message).str());
49 
50  Diag.Report(Subject->getLocStart(), DiagID) << Subject->getSourceRange();
51 }
52 
53 
54 TeslaVisitor::TeslaVisitor(llvm::StringRef Filename, ASTContext *Context)
55  : Filename(Filename), Context(Context)
56 {
57 }
58 
60  for (auto *A : Automata)
61  delete A;
62 
63  for (auto *R : Roots)
64  delete R;
65 }
66 
67 bool TeslaVisitor::VisitCallExpr(CallExpr *E) {
68  FunctionDecl *F = E->getDirectCallee();
69  if (!F) return true;
70 
71  StringRef FnName = F->getName();
72  if (!FnName.startswith(TESLA_BASE)) return true;
73 
74  // TESLA function calls might be inline assertions.
75  if (FnName == INLINE_ASSERTION) {
76  OwningPtr<Parser> P(Parser::AssertionParser(E, *Context));
77  if (!P)
78  return false;
79 
80  OwningPtr<AutomatonDescription> Description;
81  OwningPtr<Usage> Use;
82  if (!P->Parse(Description, Use))
83  return false;
84 
85  Automata.push_back(Description.take());
86  Roots.push_back(Use.take());
87  return true;
88  }
89 
90  return true;
91 }
92 
93 
94 bool TeslaVisitor::VisitFunctionDecl(FunctionDecl *F) {
95  // Only analyse non-deleted definitions (i.e. definitions with bodies).
96  if (!F->doesThisDeclarationHaveABody())
97  return true;
98 
99 
100  // We only parse functions that return __tesla_automaton_description*.
101  const Type *RetTy = F->getResultType().getTypePtr();
102  if (!RetTy->isPointerType())
103  return true;
104 
105  QualType Pointee = RetTy->getPointeeType();
106  auto TypeID = Pointee.getBaseTypeIdentifier();
107  if (!TypeID)
108  return true;
109 
110  OwningPtr<Parser> P;
111  StringRef FnName = F->getName();
112 
113  // Build a Parser appropriate to what we're parsing.
114  string RetTypeName = TypeID->getName();
115  if (RetTypeName == AUTOMATON_DESC)
116  P.reset(Parser::AutomatonParser(F, *Context));
117 
118  else if ((RetTypeName == AUTOMATON_USAGE) && (FnName != AUTOMATON_USES))
119  P.reset(Parser::MappingParser(F, *Context));
120 
121  else
122  return true;
123 
124 
125  // Actually parse the function.
126  if (!P)
127  return false;
128 
129  OwningPtr<AutomatonDescription> Description;
130  OwningPtr<Usage> Use;
131  if (!P->Parse(Description, Use))
132  return false;
133 
134  if (Description)
135  Automata.push_back(Description.take());
136 
137  if (Use)
138  Roots.push_back(Use.take());
139 
140  return true;
141 }
142 
143 } // namespace tesla