- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
          [clang-tidy][NFC] Do less unnecessary work in modernize-deprecated-headers
          #160967
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    Conversation
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
    | 
          
 @llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Victor Chernyakin (localspook) Changes
 Full diff: https://github.com/llvm/llvm-project/pull/160967.diff 2 Files Affected: 
 diff --git a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp
index 9f4c215614287..01aa9feb27e57 100644
--- a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp
@@ -24,8 +24,9 @@ namespace {
 class IncludeModernizePPCallbacks : public PPCallbacks {
 public:
   explicit IncludeModernizePPCallbacks(
-      std::vector<IncludeMarker> &IncludesToBeProcessed, LangOptions LangOpts,
-      const SourceManager &SM, bool CheckHeaderFile);
+      std::vector<IncludeMarker> &IncludesToBeProcessed,
+      const LangOptions &LangOpts, const SourceManager &SM,
+      bool CheckHeaderFile);
 
   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
                           StringRef FileName, bool IsAngled,
@@ -37,8 +38,7 @@ class IncludeModernizePPCallbacks : public PPCallbacks {
 
 private:
   std::vector<IncludeMarker> &IncludesToBeProcessed;
-  LangOptions LangOpts;
-  llvm::StringMap<std::string> CStyledHeaderToCxx;
+  llvm::StringMap<StringRef> CStyledHeaderToCxx;
   llvm::StringSet<> DeleteHeaders;
   const SourceManager &SM;
   bool CheckHeaderFile;
@@ -131,48 +131,38 @@ void DeprecatedHeadersCheck::check(
 }
 
 IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(
-    std::vector<IncludeMarker> &IncludesToBeProcessed, LangOptions LangOpts,
-    const SourceManager &SM, bool CheckHeaderFile)
-    : IncludesToBeProcessed(IncludesToBeProcessed), LangOpts(LangOpts), SM(SM),
+    std::vector<IncludeMarker> &IncludesToBeProcessed,
+    const LangOptions &LangOpts, const SourceManager &SM, bool CheckHeaderFile)
+    : IncludesToBeProcessed(IncludesToBeProcessed), SM(SM),
       CheckHeaderFile(CheckHeaderFile) {
-  for (const auto &KeyValue :
-       std::vector<std::pair<llvm::StringRef, std::string>>(
-           {{"assert.h", "cassert"},
-            {"complex.h", "complex"},
-            {"ctype.h", "cctype"},
-            {"errno.h", "cerrno"},
-            {"float.h", "cfloat"},
-            {"limits.h", "climits"},
-            {"locale.h", "clocale"},
-            {"math.h", "cmath"},
-            {"setjmp.h", "csetjmp"},
-            {"signal.h", "csignal"},
-            {"stdarg.h", "cstdarg"},
-            {"stddef.h", "cstddef"},
-            {"stdio.h", "cstdio"},
-            {"stdlib.h", "cstdlib"},
-            {"string.h", "cstring"},
-            {"time.h", "ctime"},
-            {"wchar.h", "cwchar"},
-            {"wctype.h", "cwctype"}})) {
+
+  static constexpr std::pair<StringRef, StringRef> CXX98Headers[] = {
+      {"assert.h", "cassert"}, {"complex.h", "complex"},
+      {"ctype.h", "cctype"},   {"errno.h", "cerrno"},
+      {"float.h", "cfloat"},   {"limits.h", "climits"},
+      {"locale.h", "clocale"}, {"math.h", "cmath"},
+      {"setjmp.h", "csetjmp"}, {"signal.h", "csignal"},
+      {"stdarg.h", "cstdarg"}, {"stddef.h", "cstddef"},
+      {"stdio.h", "cstdio"},   {"stdlib.h", "cstdlib"},
+      {"string.h", "cstring"}, {"time.h", "ctime"},
+      {"wchar.h", "cwchar"},   {"wctype.h", "cwctype"},
+  };
+  for (const auto &KeyValue : CXX98Headers)
     CStyledHeaderToCxx.insert(KeyValue);
-  }
-  // Add C++11 headers.
-  if (LangOpts.CPlusPlus11) {
-    for (const auto &KeyValue :
-         std::vector<std::pair<llvm::StringRef, std::string>>(
-             {{"fenv.h", "cfenv"},
-              {"stdint.h", "cstdint"},
-              {"inttypes.h", "cinttypes"},
-              {"tgmath.h", "ctgmath"},
-              {"uchar.h", "cuchar"}})) {
+
+  static constexpr std::pair<StringRef, StringRef> CXX11Headers[] = {
+      {"fenv.h", "cfenv"},         {"stdint.h", "cstdint"},
+      {"inttypes.h", "cinttypes"}, {"tgmath.h", "ctgmath"},
+      {"uchar.h", "cuchar"},
+  };
+  if (LangOpts.CPlusPlus11)
+    for (const auto &KeyValue : CXX11Headers)
       CStyledHeaderToCxx.insert(KeyValue);
-    }
-  }
-  for (const auto &Key :
-       std::vector<std::string>({"stdalign.h", "stdbool.h", "iso646.h"})) {
+
+  static constexpr StringRef HeadersToDelete[] = {"stdalign.h", "stdbool.h",
+                                                  "iso646.h"};
+  for (const auto &Key : HeadersToDelete)
     DeleteHeaders.insert(Key);
-  }
 }
 
 void IncludeModernizePPCallbacks::InclusionDirective(
@@ -205,7 +195,7 @@ void IncludeModernizePPCallbacks::InclusionDirective(
   } else if (DeleteHeaders.contains(FileName)) {
     IncludesToBeProcessed.emplace_back(
         // NOLINTNEXTLINE(modernize-use-emplace) - false-positive
-        IncludeMarker{std::string{}, FileName,
+        IncludeMarker{StringRef{}, FileName,
                       SourceRange{HashLoc, FilenameRange.getEnd()}, DiagLoc});
   }
 }
diff --git a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.h b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.h
index c9409cb641c54..03cf433aa2809 100644
--- a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.h
@@ -44,7 +44,7 @@ class DeprecatedHeadersCheck : public ClangTidyCheck {
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 
   struct IncludeMarker {
-    std::string Replacement;
+    StringRef Replacement;
     StringRef FileName;
     SourceRange ReplacementRange;
     SourceLocation DiagLoc;
 | 
    
              
                    HerrCai0907
  
              
              approved these changes
              
                  
                    Sep 29, 2025 
                  
              
              
            
            
              
                    vbvictor
  
              
              approved these changes
              
                  
                    Oct 1, 2025 
                  
              
              
            
            
              
                    nicovank
  
              
              approved these changes
              
                  
                    Oct 2, 2025 
                  
              
              
            
            
        
          
                clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp
              
                Outdated
          
            Show resolved
            Hide resolved
        
      
    
  svkeerthy 
      pushed a commit
      that referenced
      this pull request
    
      Oct 9, 2025 
    
    
      
  
    
      
    
  
…eaders` (#160967) - `IncludeModernizePPCallbacks` creates temporary vectors when all it needs is constant arrays - The header replacement strings are `std::string` when they can just be `StringRef` - `IncludeModernizePPCallbacks`'s constructor 1. Takes `LangOptions` by value (the thing is 832 bytes) 2. Stores it, but none of `IncludeModernizePPCallbacks`'s member functions use it
    
  clingfei 
      pushed a commit
        to clingfei/llvm-project
      that referenced
      this pull request
    
      Oct 10, 2025 
    
    
      
  
    
      
    
  
…eaders` (llvm#160967) - `IncludeModernizePPCallbacks` creates temporary vectors when all it needs is constant arrays - The header replacement strings are `std::string` when they can just be `StringRef` - `IncludeModernizePPCallbacks`'s constructor 1. Takes `LangOptions` by value (the thing is 832 bytes) 2. Stores it, but none of `IncludeModernizePPCallbacks`'s member functions use it
    
  akadutta 
      pushed a commit
        to akadutta/llvm-project
      that referenced
      this pull request
    
      Oct 14, 2025 
    
    
      
  
    
      
    
  
…eaders` (llvm#160967) - `IncludeModernizePPCallbacks` creates temporary vectors when all it needs is constant arrays - The header replacement strings are `std::string` when they can just be `StringRef` - `IncludeModernizePPCallbacks`'s constructor 1. Takes `LangOptions` by value (the thing is 832 bytes) 2. Stores it, but none of `IncludeModernizePPCallbacks`'s member functions use it
    
  espressif-bot 
      pushed a commit
        to espressif/llvm-project
      that referenced
      this pull request
    
      Oct 23, 2025 
    
    
      
  
    
      
    
  
…eaders` (llvm#160967) - `IncludeModernizePPCallbacks` creates temporary vectors when all it needs is constant arrays - The header replacement strings are `std::string` when they can just be `StringRef` - `IncludeModernizePPCallbacks`'s constructor 1. Takes `LangOptions` by value (the thing is 832 bytes) 2. Stores it, but none of `IncludeModernizePPCallbacks`'s member functions use it
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
      
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
IncludeModernizePPCallbackscreates temporary vectors when all it needs is constant arraysstd::stringwhen they can just beStringRefIncludeModernizePPCallbacks's constructorLangOptionsby value (the thing is 832 bytes)IncludeModernizePPCallbacks's member functions use it