ThreadSafety.h revision 402aa0698fec81e574818a0a6c2000fac0b2c4c6
1//===- ThreadSafety.h ------------------------------------------*- C++ --*-===//
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//
11// A intra-procedural analysis for thread safety (e.g. deadlocks and race
12// conditions), based off of an annotation system.
13//
14// See http://gcc.gnu.org/wiki/ThreadSafetyAnnotation for the gcc version.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_CLANG_THREADSAFETY_H
19#define LLVM_CLANG_THREADSAFETY_H
20
21#include "clang/Basic/SourceLocation.h"
22#include "llvm/ADT/StringRef.h"
23#include "clang/Analysis/AnalysisContext.h"
24#include "clang/Sema/SemaInternal.h"
25
26namespace clang {
27namespace thread_safety {
28
29enum ProtectedOperationKind {
30  POK_VarDereference,
31  POK_VarAccess,
32  POK_FunctionCall
33};
34
35enum LockKind {
36  LK_Shared,
37  LK_Exclusive
38};
39
40enum AccessKind {
41  AK_Read,
42  AK_Written
43};
44
45class ThreadSafetyHandler {
46public:
47  typedef llvm::StringRef Name;
48  ThreadSafetyHandler() {}
49  virtual ~ThreadSafetyHandler() {}
50  virtual void handleUnmatchedUnlock(Name LockName, SourceLocation Loc) {}
51  virtual void handleDoubleLock(Name LockName, SourceLocation Loc) {}
52  virtual void handleMutexHeldEndOfScope(Name LockName, SourceLocation Loc){}
53  virtual void handleNoLockLoopEntry(Name LockName, SourceLocation Loc) {}
54  virtual void handleNoUnlock(Name LockName, Name FunName,
55                              SourceLocation Loc) {}
56  virtual void handleExclusiveAndShared(Name LockName, SourceLocation Loc1,
57                                        SourceLocation Loc2) {}
58  virtual void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK,
59                                 AccessKind AK, SourceLocation Loc) {}
60  virtual void handleMutexNotHeld(const NamedDecl *D,
61                                  ProtectedOperationKind POK, Name LockName,
62                                  LockKind LK, SourceLocation Loc) {}
63  virtual void handleFunExcludesLock(Name FunName, Name LockName,
64                                     SourceLocation Loc) {}
65};
66
67void runThreadSafetyAnalysis(AnalysisContext &AC, ThreadSafetyHandler &Handler);
68LockKind getLockKindFromAccessKind(AccessKind AK);
69
70}} // end namespace clang::thread_safety
71#endif
72