13
13
//
14
14
// ===----------------------------------------------------------------------===//
15
15
16
- #include " llvm/ADT/Twine.h"
17
- #include " llvm/DerivedTypes.h"
18
- #include " llvm/Module.h"
19
16
#include " llvm/Pass.h"
20
-
21
- #include " llvm/Support/raw_ostream.h"
17
+ #include " llvm/ADT/Twine.h"
22
18
#include " llvm/Analysis/NaCl.h"
19
+ #include " llvm/IR/DerivedTypes.h"
20
+ #include " llvm/IR/Module.h"
21
+ #include " llvm/Support/raw_ostream.h"
23
22
24
23
#include " PNaClABITypeChecker.h"
25
24
using namespace llvm ;
@@ -29,13 +28,23 @@ namespace {
29
28
class PNaClABIVerifyModule : public ModulePass {
30
29
public:
31
30
static char ID;
32
- PNaClABIVerifyModule ();
31
+ PNaClABIVerifyModule () : ModulePass(ID),
32
+ Reporter (new PNaClABIErrorReporter),
33
+ ReporterIsOwned(true ) {}
34
+ explicit PNaClABIVerifyModule (PNaClABIErrorReporter *Reporter_) :
35
+ ModulePass(ID),
36
+ Reporter(Reporter_),
37
+ ReporterIsOwned(false ) {}
38
+ ~PNaClABIVerifyModule () {
39
+ if (ReporterIsOwned)
40
+ delete Reporter;
41
+ }
33
42
bool runOnModule (Module &M);
34
43
virtual void print (raw_ostream &O, const Module *M) const ;
35
44
private:
36
45
PNaClABITypeChecker TC;
37
- std::string ErrorsString ;
38
- raw_string_ostream Errors ;
46
+ PNaClABIErrorReporter *Reporter ;
47
+ bool ReporterIsOwned ;
39
48
};
40
49
41
50
static const char *linkageName (GlobalValue::LinkageTypes LT) {
@@ -66,26 +75,24 @@ static const char *linkageName(GlobalValue::LinkageTypes LT) {
66
75
67
76
} // end anonymous namespace
68
77
69
- PNaClABIVerifyModule::PNaClABIVerifyModule () : ModulePass(ID),
70
- Errors(ErrorsString) {}
71
-
72
78
bool PNaClABIVerifyModule::runOnModule (Module &M) {
73
79
for (Module::const_global_iterator MI = M.global_begin (), ME = M.global_end ();
74
80
MI != ME; ++MI) {
75
81
// Check types of global variables and their initializers
76
82
if (!TC.isValidType (MI->getType ())) {
77
83
// GVs are pointers, so print the pointed-to type for clarity
78
- Errors << " Variable " + MI->getName () +
79
- " has disallowed type: " +
84
+ Reporter-> addError () << " Variable " << MI->getName () <<
85
+ " has disallowed type: " <<
80
86
PNaClABITypeChecker::getTypeName (MI->getType ()->getContainedType (0 ))
81
87
+ " \n " ;
82
88
} else if (MI->hasInitializer ()) {
83
89
// If the type of the global is bad, no point in checking its initializer
84
90
Type *T = TC.checkTypesInConstant (MI->getInitializer ());
85
- if (T)
86
- Errors << " Initializer for " + MI->getName () +
87
- " has disallowed type: " +
88
- PNaClABITypeChecker::getTypeName (T) + " \n " ;
91
+ if (T) {
92
+ Reporter->addError () << " Initializer for " << MI->getName () <<
93
+ " has disallowed type: " <<
94
+ PNaClABITypeChecker::getTypeName (T) << " \n " ;
95
+ }
89
96
}
90
97
91
98
// Check GV linkage types
@@ -96,54 +103,65 @@ bool PNaClABIVerifyModule::runOnModule(Module &M) {
96
103
case GlobalValue::PrivateLinkage:
97
104
break ;
98
105
default :
99
- Errors << " Variable " + MI->getName () +
100
- " has disallowed linkage type: " +
101
- linkageName (MI->getLinkage ()) + " \n " ;
106
+ Reporter-> addError () << " Variable " << MI->getName () <<
107
+ " has disallowed linkage type: " <<
108
+ linkageName (MI->getLinkage ()) << " \n " ;
102
109
}
103
110
}
104
111
// No aliases allowed for now.
105
112
for (Module::alias_iterator MI = M.alias_begin (),
106
- E = M.alias_end (); MI != E; ++MI)
107
- Errors << " Variable " + MI->getName () + " is an alias (disallowed)\n " ;
113
+ E = M.alias_end (); MI != E; ++MI) {
114
+ Reporter->addError () << " Variable " << MI->getName () <<
115
+ " is an alias (disallowed)\n " ;
116
+ }
108
117
109
118
for (Module::iterator MI = M.begin (), ME = M.end (); MI != ME; ++MI) {
110
119
// Check types of functions and their arguments
111
120
FunctionType *FT = MI->getFunctionType ();
112
- if (!TC.isValidType (FT->getReturnType ()))
113
- Errors << " Function " + MI->getName () + " has disallowed return type: " +
114
- PNaClABITypeChecker::getTypeName (FT->getReturnType ()) + " \n " ;
121
+ if (!TC.isValidType (FT->getReturnType ())) {
122
+ Reporter->addError () << " Function " << MI->getName () <<
123
+ " has disallowed return type: " <<
124
+ PNaClABITypeChecker::getTypeName (FT->getReturnType ()) << " \n " ;
125
+ }
115
126
for (unsigned I = 0 , E = FT->getNumParams (); I < E; ++I) {
116
127
Type *PT = FT->getParamType (I);
117
- if (!TC.isValidType (PT))
118
- Errors << " Function " << MI->getName () << " argument " << I + 1 <<
119
- " has disallowed type: " <<
120
- PNaClABITypeChecker::getTypeName (PT) + " \n " ;
128
+ if (!TC.isValidType (PT)) {
129
+ Reporter->addError () << " Function " << MI->getName () << " argument " <<
130
+ I + 1 << " has disallowed type: " <<
131
+ PNaClABITypeChecker::getTypeName (PT) << " \n " ;
132
+ }
121
133
}
122
134
}
123
135
124
136
// Check named metadata nodes
125
137
for (Module::const_named_metadata_iterator I = M.named_metadata_begin (),
126
138
E = M.named_metadata_end (); I != E; ++I) {
127
139
for (unsigned i = 0 , e = I->getNumOperands (); i != e; i++) {
128
- if (Type *T = TC.checkTypesInMDNode (I->getOperand (i)))
129
- Errors << " Named metadata node " + I->getName () +
130
- " refers to disallowed type: " +
131
- PNaClABITypeChecker::getTypeName (T) + " \n " ;
140
+ if (Type *T = TC.checkTypesInMDNode (I->getOperand (i))) {
141
+ Reporter->addError () << " Named metadata node " << I->getName () <<
142
+ " refers to disallowed type: " <<
143
+ PNaClABITypeChecker::getTypeName (T) << " \n " ;
144
+ }
132
145
}
133
146
}
134
- Errors.flush ();
135
147
return false ;
136
148
}
137
149
150
+ // This method exists so that the passes can easily be run with opt -analyze.
151
+ // In this case the default constructor is used and we want to reset the error
152
+ // messages after each print (this is more of an issue for the FunctionPass
153
+ // than the ModulePass)
138
154
void PNaClABIVerifyModule::print (llvm::raw_ostream &O, const Module *M) const {
139
- O << ErrorsString;
155
+ Reporter->printErrors (O);
156
+ Reporter->reset ();
140
157
}
141
158
142
159
char PNaClABIVerifyModule::ID = 0 ;
143
160
144
161
static RegisterPass<PNaClABIVerifyModule> X (" verify-pnaclabi-module" ,
145
162
" Verify module for PNaCl" , false , false );
146
163
147
- ModulePass *llvm::createPNaClABIVerifyModulePass () {
148
- return new PNaClABIVerifyModule ();
164
+ ModulePass *llvm::createPNaClABIVerifyModulePass (
165
+ PNaClABIErrorReporter *Reporter) {
166
+ return new PNaClABIVerifyModule (Reporter);
149
167
}
0 commit comments