Skip to content

Conversation

@jansvoboda11
Copy link
Contributor

This PR makes CompilerInvocation the sole owner of the AnalyzerOptions instance. Clients can no longer become co-owners by doing something like this:

void shareOwnership(CompilerInvocation &CI) {
  IntrusiveRefCntPtr Shared = &CI.getAnalyzerOpts();
}

The motivation for this is given here: #133467 (comment)

This PR makes `CompilerInvocation` the sole owner of the `AnalyzerOptions` instance. Clients can no longer become co-owners by doing something like this:

```c++
void shareOwnership(CompilerInvocation &CI) {
  IntrusiveRefCntPtr Shared = &CI.getAnalyzerOpts();
}
```
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Apr 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 28, 2025

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Jan Svoboda (jansvoboda11)

Changes

This PR makes CompilerInvocation the sole owner of the AnalyzerOptions instance. Clients can no longer become co-owners by doing something like this:

void shareOwnership(CompilerInvocation &CI) {
  IntrusiveRefCntPtr Shared = &CI.getAnalyzerOpts();
}

The motivation for this is given here: #133467 (comment)


Full diff: https://github.com/llvm/llvm-project/pull/137680.diff

3 Files Affected:

  • (modified) clang/include/clang/Frontend/CompilerInvocation.h (+1-1)
  • (modified) clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h (+1-3)
  • (modified) clang/lib/Frontend/CompilerInvocation.cpp (+2-2)
diff --git a/clang/include/clang/Frontend/CompilerInvocation.h b/clang/include/clang/Frontend/CompilerInvocation.h
index 1827ff0f6816d..f2c653d3075df 100644
--- a/clang/include/clang/Frontend/CompilerInvocation.h
+++ b/clang/include/clang/Frontend/CompilerInvocation.h
@@ -89,7 +89,7 @@ class CompilerInvocationBase {
   std::shared_ptr<PreprocessorOptions> PPOpts;
 
   /// Options controlling the static analyzer.
-  AnalyzerOptionsRef AnalyzerOpts;
+  std::shared_ptr<AnalyzerOptions> AnalyzerOpts;
 
   std::shared_ptr<MigratorOptions> MigratorOpts;
 
diff --git a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
index 2c970301879d2..54c2fb8a60ca1 100644
--- a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
+++ b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
@@ -176,7 +176,7 @@ class PositiveAnalyzerOption {
 /// and should be eventually converted into -analyzer-config flags. New analyzer
 /// options should not be implemented as frontend flags. Frontend flags still
 /// make sense for things that do not affect the actual analysis.
-class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
+class AnalyzerOptions {
 public:
   using ConfigTable = llvm::StringMap<std::string>;
 
@@ -416,8 +416,6 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
   }
 };
 
-using AnalyzerOptionsRef = IntrusiveRefCntPtr<AnalyzerOptions>;
-
 //===----------------------------------------------------------------------===//
 // We'll use AnalyzerOptions in the frontend, but we can't link the frontend
 // with clangStaticAnalyzerCore, because clangStaticAnalyzerCore depends on
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 1df503859204d..8ff62ae2552c3 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -142,7 +142,7 @@ CompilerInvocationBase::CompilerInvocationBase()
       DiagnosticOpts(llvm::makeIntrusiveRefCnt<DiagnosticOptions>()),
       HSOpts(std::make_shared<HeaderSearchOptions>()),
       PPOpts(std::make_shared<PreprocessorOptions>()),
-      AnalyzerOpts(llvm::makeIntrusiveRefCnt<AnalyzerOptions>()),
+      AnalyzerOpts(std::make_shared<AnalyzerOptions>()),
       MigratorOpts(std::make_shared<MigratorOptions>()),
       APINotesOpts(std::make_shared<APINotesOptions>()),
       CodeGenOpts(std::make_shared<CodeGenOptions>()),
@@ -159,7 +159,7 @@ CompilerInvocationBase::deep_copy_assign(const CompilerInvocationBase &X) {
     DiagnosticOpts = makeIntrusiveRefCntCopy(X.getDiagnosticOpts());
     HSOpts = make_shared_copy(X.getHeaderSearchOpts());
     PPOpts = make_shared_copy(X.getPreprocessorOpts());
-    AnalyzerOpts = makeIntrusiveRefCntCopy(X.getAnalyzerOpts());
+    AnalyzerOpts = make_shared_copy(X.getAnalyzerOpts());
     MigratorOpts = make_shared_copy(X.getMigratorOpts());
     APINotesOpts = make_shared_copy(X.getAPINotesOpts());
     CodeGenOpts = make_shared_copy(X.getCodeGenOpts());

@llvmbot
Copy link
Member

llvmbot commented Apr 28, 2025

@llvm/pr-subscribers-clang

Author: Jan Svoboda (jansvoboda11)

Changes

This PR makes CompilerInvocation the sole owner of the AnalyzerOptions instance. Clients can no longer become co-owners by doing something like this:

void shareOwnership(CompilerInvocation &amp;CI) {
  IntrusiveRefCntPtr Shared = &amp;CI.getAnalyzerOpts();
}

The motivation for this is given here: #133467 (comment)


Full diff: https://github.com/llvm/llvm-project/pull/137680.diff

3 Files Affected:

  • (modified) clang/include/clang/Frontend/CompilerInvocation.h (+1-1)
  • (modified) clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h (+1-3)
  • (modified) clang/lib/Frontend/CompilerInvocation.cpp (+2-2)
diff --git a/clang/include/clang/Frontend/CompilerInvocation.h b/clang/include/clang/Frontend/CompilerInvocation.h
index 1827ff0f6816d..f2c653d3075df 100644
--- a/clang/include/clang/Frontend/CompilerInvocation.h
+++ b/clang/include/clang/Frontend/CompilerInvocation.h
@@ -89,7 +89,7 @@ class CompilerInvocationBase {
   std::shared_ptr<PreprocessorOptions> PPOpts;
 
   /// Options controlling the static analyzer.
-  AnalyzerOptionsRef AnalyzerOpts;
+  std::shared_ptr<AnalyzerOptions> AnalyzerOpts;
 
   std::shared_ptr<MigratorOptions> MigratorOpts;
 
diff --git a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
index 2c970301879d2..54c2fb8a60ca1 100644
--- a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
+++ b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
@@ -176,7 +176,7 @@ class PositiveAnalyzerOption {
 /// and should be eventually converted into -analyzer-config flags. New analyzer
 /// options should not be implemented as frontend flags. Frontend flags still
 /// make sense for things that do not affect the actual analysis.
-class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
+class AnalyzerOptions {
 public:
   using ConfigTable = llvm::StringMap<std::string>;
 
@@ -416,8 +416,6 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
   }
 };
 
-using AnalyzerOptionsRef = IntrusiveRefCntPtr<AnalyzerOptions>;
-
 //===----------------------------------------------------------------------===//
 // We'll use AnalyzerOptions in the frontend, but we can't link the frontend
 // with clangStaticAnalyzerCore, because clangStaticAnalyzerCore depends on
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 1df503859204d..8ff62ae2552c3 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -142,7 +142,7 @@ CompilerInvocationBase::CompilerInvocationBase()
       DiagnosticOpts(llvm::makeIntrusiveRefCnt<DiagnosticOptions>()),
       HSOpts(std::make_shared<HeaderSearchOptions>()),
       PPOpts(std::make_shared<PreprocessorOptions>()),
-      AnalyzerOpts(llvm::makeIntrusiveRefCnt<AnalyzerOptions>()),
+      AnalyzerOpts(std::make_shared<AnalyzerOptions>()),
       MigratorOpts(std::make_shared<MigratorOptions>()),
       APINotesOpts(std::make_shared<APINotesOptions>()),
       CodeGenOpts(std::make_shared<CodeGenOptions>()),
@@ -159,7 +159,7 @@ CompilerInvocationBase::deep_copy_assign(const CompilerInvocationBase &X) {
     DiagnosticOpts = makeIntrusiveRefCntCopy(X.getDiagnosticOpts());
     HSOpts = make_shared_copy(X.getHeaderSearchOpts());
     PPOpts = make_shared_copy(X.getPreprocessorOpts());
-    AnalyzerOpts = makeIntrusiveRefCntCopy(X.getAnalyzerOpts());
+    AnalyzerOpts = make_shared_copy(X.getAnalyzerOpts());
     MigratorOpts = make_shared_copy(X.getMigratorOpts());
     APINotesOpts = make_shared_copy(X.getAPINotesOpts());
     CodeGenOpts = make_shared_copy(X.getCodeGenOpts());

Copy link
Contributor

@steakhal steakhal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Please make sure not only clang but also clang-tidy tests pass.

@jansvoboda11 jansvoboda11 merged commit cebaf0c into llvm:main Apr 28, 2025
14 checks passed
@jansvoboda11 jansvoboda11 deleted the analyzer-opts-ref-count branch April 28, 2025 19:42
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…m#137680)

This PR makes `CompilerInvocation` the sole owner of the
`AnalyzerOptions` instance. Clients can no longer become co-owners by
doing something like this:

```c++
void shareOwnership(CompilerInvocation &CI) {
  IntrusiveRefCntPtr Shared = &CI.getAnalyzerOpts();
}
```

The motivation for this is given here:
llvm#133467 (comment)
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
…m#137680)

This PR makes `CompilerInvocation` the sole owner of the
`AnalyzerOptions` instance. Clients can no longer become co-owners by
doing something like this:

```c++
void shareOwnership(CompilerInvocation &CI) {
  IntrusiveRefCntPtr Shared = &CI.getAnalyzerOpts();
}
```

The motivation for this is given here:
llvm#133467 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:static analyzer clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants