<commit_before>//===- TreeView.cpp - diagtool tool for printing warning flags ------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This diagnostic tool 
//
//===----------------------------------------------------------------------===//

#include "DiagTool.h"
#include "DiagnosticNames.h"
#include "clang/AST/ASTDiagnostic.h"
#include "clang/Basic/AllDiagnostics.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Process.h"

DEF_DIAGTOOL("tree",
             "Show warning flags in a tree view",
             TreeView)
  
using namespace clang;
using namespace diagtool;

static void printUsage() {
  llvm::errs() << "Usage: diagtool tree [--flags-only] [<diagnostic-group>]\n";
}

static bool showColors(llvm::raw_ostream &out) {
  if (&out != &llvm::errs() && &out != &llvm::outs())
    return false;
  return llvm::errs().is_displayed() && llvm::outs().is_displayed();
}

static void setColor(bool ShowColors, llvm::raw_ostream &out,
                     llvm::raw_ostream::Colors Color) {
  if (ShowColors)
    out << llvm::sys::Process::OutputColor(Color, false, false);
}

static void resetColor(bool ShowColors, llvm::raw_ostream &out) {
  if (ShowColors)
    out << llvm::sys::Process::ResetColor();
}

static clang::DiagnosticsEngine::Level getLevel(unsigned DiagID) {
  // FIXME: This feels like a hack.
  static clang::DiagnosticsEngine Diags(new DiagnosticIDs,
                                        new DiagnosticOptions);
  return Diags.getDiagnosticLevel(DiagID, SourceLocation());
}

static void printGroup(llvm::raw_ostream &out, const GroupRecord &Group,
                       bool FlagsOnly, unsigned Indent = 0) {
  out.indent(Indent * 2);
  
  bool ShowColors = showColors(out);
  setColor(ShowColors, out, llvm::raw_ostream::YELLOW);
  out << "-W" << Group.getName() << "\n";
  resetColor(ShowColors, out);
  
  ++Indent;
  for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
                                      E = Group.subgroup_end();
       I != E; ++I) {
    printGroup(out, *I, FlagsOnly, Indent);
  }

  if (!FlagsOnly) {
    for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
                                           E = Group.diagnostics_end();
         I != E; ++I) {
      if (ShowColors) {
        if (getLevel(I->DiagID) != DiagnosticsEngine::Ignored) {
          setColor(ShowColors, out, llvm::raw_ostream::GREEN);
        }
      }
      out.indent(Indent * 2);
      out << I->getName();
      resetColor(ShowColors, out);
      out << "\n";
    }
  }
}

static int showGroup(llvm::raw_ostream &out, StringRef RootGroup,
                     bool FlagsOnly) {
  ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
<commit_msg>diagtool: simplify TreeView diagnostic classification<commit_after>//===- TreeView.cpp - diagtool tool for printing warning flags ------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This diagnostic tool 
//
//===----------------------------------------------------------------------===//

#include "DiagTool.h"
#include "DiagnosticNames.h"
#include "clang/AST/ASTDiagnostic.h"
#include "clang/Basic/AllDiagnostics.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Process.h"

DEF_DIAGTOOL("tree",
             "Show warning flags in a tree view",
             TreeView)
  
using namespace clang;
using namespace diagtool;

static void printUsage() {
  llvm::errs() << "Usage: diagtool tree [--flags-only] [<diagnostic-group>]\n";
}

static bool showColors(llvm::raw_ostream &out) {
  if (&out != &llvm::errs() && &out != &llvm::outs())
    return false;
  return llvm::errs().is_displayed() && llvm::outs().is_displayed();
}

static void setColor(bool ShowColors, llvm::raw_ostream &out,
                     llvm::raw_ostream::Colors Color) {
  if (ShowColors)
    out << llvm::sys::Process::OutputColor(Color, false, false);
}

static void resetColor(bool ShowColors, llvm::raw_ostream &out) {
  if (ShowColors)
    out << llvm::sys::Process::ResetColor();
}

static bool isIgnored(unsigned DiagID) {
  // FIXME: This feels like a hack.
  static clang::DiagnosticsEngine Diags(new DiagnosticIDs,
                                        new DiagnosticOptions);
  return Diags.isIgnored(DiagID, SourceLocation());
}

static void printGroup(llvm::raw_ostream &out, const GroupRecord &Group,
                       bool FlagsOnly, unsigned Indent = 0) {
  out.indent(Indent * 2);
  
  bool ShowColors = showColors(out);
  setColor(ShowColors, out, llvm::raw_ostream::YELLOW);
  out << "-W" << Group.getName() << "\n";
  resetColor(ShowColors, out);
  
  ++Indent;
  for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
                                      E = Group.subgroup_end();
       I != E; ++I) {
    printGroup(out, *I, FlagsOnly, Indent);
  }

  if (!FlagsOnly) {
    for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
                                           E = Group.diagnostics_end();
         I != E; ++I) {
      if (ShowColors) {
        if (!isIgnored(I->DiagID)) {
          setColor(ShowColors, out, llvm::raw_ostream::GREEN);
        }
      }
      out.indent(Indent * 2);
      out << I->getName();
      resetColor(ShowColors, out);
      out << "\n";
    }
  }
}

static int showGroup(llvm::raw_ostream &out, StringRef RootGroup,
                     bool FlagsOnly) {
  ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();

{'fraction_non_alphanumeric': 0.18976441917140535, 'fraction_numerical': 0.0009748172217709179, 'mean_word_length': 2.4143094841930117, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4710740', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1849, 'n_words': 423}