[cfe-commits] r97831 - in /cfe/trunk: include/clang/Frontend/ASTUnit.h lib/Frontend/ASTUnit.cpp tools/CIndex/CIndex.cpp

Douglas Gregor dgregor at apple.com
Fri Mar 5 13:16:26 PST 2010


Author: dgregor
Date: Fri Mar  5 15:16:25 2010
New Revision: 97831

URL: http://llvm.org/viewvc/llvm-project?rev=97831&view=rev
Log:
A little hack to identify unwanted concurrency in CIndex

Modified:
    cfe/trunk/include/clang/Frontend/ASTUnit.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/tools/CIndex/CIndex.cpp

Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=97831&r1=97830&r2=97831&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Fri Mar  5 15:16:25 2010
@@ -88,11 +88,49 @@
   /// \brief Temporary files that should be removed when the ASTUnit is 
   /// destroyed.
   llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
+
+#ifndef NDEBUG
+  /// \brief Simple hack to allow us to assert that ASTUnit is not being
+  /// used concurrently, which is not supported.
+  ///
+  /// Clients should create instances of the ConcurrencyCheck class whenever
+  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
+  /// just about any usage.
+  unsigned int ConcurrencyCheckValue;
+  static const unsigned int CheckLocked = 28573289;
+  static const unsigned int CheckUnlocked = 9803453;
+#endif
   
   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
-
+  
 public:
+  class ConcurrencyCheck {
+#ifndef NDEBUG
+    volatile ASTUnit &Self;
+#endif
+    
+  public:
+    explicit ConcurrencyCheck(ASTUnit &Self)
+#ifndef NDEBUG
+      : Self(Self) 
+#endif
+    { 
+#ifndef NDEBUG
+      assert(Self.ConcurrencyCheckValue == CheckUnlocked && 
+             "Concurrent access to ASTUnit!");
+      Self.ConcurrencyCheckValue = CheckLocked;
+#endif
+    }
+    
+#ifndef NDEBUG
+    ~ConcurrencyCheck() {
+      Self.ConcurrencyCheckValue = CheckUnlocked;
+    }
+#endif
+  };
+  friend class ConcurrencyCheck;
+  
   ASTUnit(bool MainFileIsAST);
   ~ASTUnit();
 

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=97831&r1=97830&r2=97831&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Fri Mar  5 15:16:25 2010
@@ -36,9 +36,12 @@
 using namespace clang;
 
 ASTUnit::ASTUnit(bool _MainFileIsAST)
-  : MainFileIsAST(_MainFileIsAST) {
+  : MainFileIsAST(_MainFileIsAST), ConcurrencyCheckValue(CheckUnlocked) {
 }
 ASTUnit::~ASTUnit() {
+#ifndef NDEBUG
+  ConcurrencyCheckValue = CheckLocked;
+#endif
   for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
     TemporaryFiles[I].eraseFromDisk();
 }

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=97831&r1=97830&r2=97831&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Fri Mar  5 15:16:25 2010
@@ -1527,6 +1527,8 @@
 
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
 
+  ASTUnit::ConcurrencyCheck Check(*CXXUnit);
+
   SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
   CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
   if (SLoc.isValid()) {
@@ -2052,6 +2054,8 @@
   if (!CXXUnit || !Tokens || !NumTokens)
     return;
 
+  ASTUnit::ConcurrencyCheck Check(*CXXUnit);
+  
   SourceRange R = cxloc::translateCXSourceRange(Range);
   if (R.isInvalid())
     return;
@@ -2175,6 +2179,8 @@
   if (!CXXUnit || !Tokens)
     return;
 
+  ASTUnit::ConcurrencyCheck Check(*CXXUnit);
+
   // Annotate all of the source locations in the region of interest that map
   SourceRange RegionOfInterest;
   RegionOfInterest.setBegin(





More information about the cfe-commits mailing list