Computer Laboratory

Annotations.h
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 #ifndef TESLA_ANNOTATIONS_H
33 #define TESLA_ANNOTATIONS_H
34 
35 #include <llvm/ADT/StringRef.h>
36 #include <llvm/IR/Instructions.h>
37 
38 #include <string>
39 
40 namespace llvm {
41  class APInt;
42  class User;
43  class Value;
44 }
45 
46 namespace tesla {
47 
50 public:
51  virtual ~PtrAnnotation() {}
52 
54  virtual AnnotationKind getKind() const { return AK_RawAnnotation; }
55  static bool classof(const PtrAnnotation *P) { return true; }
56 
57  static PtrAnnotation* Interpret(llvm::User*);
58  llvm::StringRef getName() const { return Name; }
59  const llvm::Value* getValue() const { return Call; }
60 
61 protected:
62  PtrAnnotation(llvm::CallInst *Call, const llvm::Value *PtrArg,
63  llvm::StringRef Name, llvm::StringRef Filename,
64  llvm::APInt& Line)
65  : Call(Call), PtrArg(PtrArg), Name(Name), Filename(Filename), Line(Line)
66  {
67  }
68 
69  static llvm::StringRef ExtractStringConstant(const llvm::Value *V);
70 
71  llvm::CallInst *Call;
72  const llvm::Value *PtrArg;
73  const llvm::StringRef Name;
74  const llvm::StringRef Filename;
75  const llvm::APInt& Line;
76 };
77 
78 
81 public:
82  virtual AnnotationKind getKind() const { return AK_FieldAnnotation; }
83  static bool classof(const PtrAnnotation *P) {
84  return (P->getKind() == AK_FieldAnnotation);
85  }
86 
87  llvm::StringRef getStructName() const { return StructName; }
88  llvm::StringRef getFieldName() const { return FieldName; }
89  std::string completeFieldName() const;
90 
91  llvm::Value::use_iterator begin() { return Call->use_begin(); }
92  llvm::Value::use_iterator end() { return Call->use_end(); }
93 
94  FieldAnnotation(llvm::CallInst *Call, const llvm::Value *PtrArg,
95  llvm::StringRef StructName, llvm::StringRef FieldName,
96  llvm::StringRef Filename, llvm::APInt& Line)
97  : PtrAnnotation(Call, PtrArg, (StructName + "." + FieldName).str(),
98  Filename, Line),
99  StructName(StructName), FieldName(FieldName)
100  {
101  }
102 
103 private:
104  const llvm::StringRef StructName;
105  const llvm::StringRef FieldName;
106 };
107 
108 }
109 
110 #endif // !TESLA_ANNOTATIONS_H