<commit_before>//===- lld/Core/Simple.h - Simple implementations of Atom and File --------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Provide simple implementations for Atoms and File.
///
//===----------------------------------------------------------------------===//

#ifndef LLD_CORE_SIMPLE_H
#define LLD_CORE_SIMPLE_H

#include "lld/Core/DefinedAtom.h"
#include "lld/Core/File.h"
#include "lld/Core/ArchiveLibraryFile.h"
#include "lld/Core/LinkingContext.h"
#include "lld/Core/Reference.h"
#include "lld/Core/UndefinedAtom.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"

namespace lld {

class SimpleFile : public File {
public:
  SimpleFile(StringRef path) : File(path, kindObject) {}

  void addAtom(const Atom &atom) {
    if (auto *defAtom = dyn_cast<DefinedAtom>(&atom)) {
      _definedAtoms._atoms.push_back(defAtom);
    } else if (auto *undefAtom = dyn_cast<UndefinedAtom>(&atom)) {
      _undefinedAtoms._atoms.push_back(undefAtom);
    } else if (auto *shlibAtom = dyn_cast<SharedLibraryAtom>(&atom)) {
      _sharedLibraryAtoms._atoms.push_back(shlibAtom);
    } else if (auto *absAtom = dyn_cast<AbsoluteAtom>(&atom)) {
      _absoluteAtoms._atoms.push_back(absAtom);
    } else {
      llvm_unreachable("atom has unknown definition kind");
    }
  }

  void removeDefinedAtomsIf(std::function<bool(const DefinedAtom *)> pred) {
    auto &atoms = _definedAtoms._atoms;
    auto newEnd = std::remove_if(atoms.begin(), atoms.end(), pred);
    atoms.erase(newEnd, atoms.end());
  }

  const atom_collection<DefinedAtom> &defined() const override {
    return _definedAtoms;
  }

  const atom_collection<UndefinedAtom> &undefined() const override {
    return _undefinedAtoms;
  }

  const atom_collection<SharedLibraryAtom> &sharedLibrary() const override {
    return _sharedLibraryAtoms;
  }

  const atom_collection<AbsoluteAtom> &absolute() const override {
    return _absoluteAtoms;
  }

  typedef range<std::vector<const DefinedAtom *>::iterator> DefinedAtomRange;
  DefinedAtomRange definedAtoms() { return make_range(_definedAtoms._atoms); }

private:
  atom_collection_vector<DefinedAtom>        _definedAtoms;
  atom_collection_vector<UndefinedAtom>      _undefinedAtoms;
  atom_collection_vector<SharedLibraryAtom>  _sharedLibraryAtoms;
  atom_collection_vector<AbsoluteAtom>       _absoluteAtoms;
};

/// \brief Archive library file that may be used as a virtual container
/// for symbols that should be added dynamically in response to
/// call to find() method.
class SimpleArchiveLibraryFile : public ArchiveLibraryFile {
public:
  SimpleArchiveLibraryFile(StringRef filename)
      : ArchiveLibraryFile(filename) {}

  const atom_collection<DefinedAtom> &defined() const override {
    return _definedAtoms;
  }

  const atom_collection<UndefinedAtom> &undefined() const override {
    return _undefinedAtoms;
  }

  const atom_collection<SharedLibraryAtom> &sharedLibrary() const override {
    return _sharedLibraryAtoms;
  }

  const atom_collection<AbsoluteAtom> &absolute() const override {
    return _absoluteAtoms;
  }

  File *find(StringRef sym, bool dataSymbolOnly) override {
    // For descendants:
    // do some checks here and return dynamically generated files with atoms.
    return nullptr;
  }<commit_msg>Define SimpleFile::addAtom for concrete atom types.<commit_after>//===- lld/Core/Simple.h - Simple implementations of Atom and File --------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Provide simple implementations for Atoms and File.
///
//===----------------------------------------------------------------------===//

#ifndef LLD_CORE_SIMPLE_H
#define LLD_CORE_SIMPLE_H

#include "lld/Core/DefinedAtom.h"
#include "lld/Core/File.h"
#include "lld/Core/ArchiveLibraryFile.h"
#include "lld/Core/LinkingContext.h"
#include "lld/Core/Reference.h"
#include "lld/Core/UndefinedAtom.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"

namespace lld {

class SimpleFile : public File {
public:
  SimpleFile(StringRef path) : File(path, kindObject) {}

  void addAtom(const DefinedAtom &a) { _defined._atoms.push_back(&a); }
  void addAtom(const UndefinedAtom &a) { _undefined._atoms.push_back(&a); }
  void addAtom(const SharedLibraryAtom &a) { _shared._atoms.push_back(&a); }
  void addAtom(const AbsoluteAtom &a) { _absolute._atoms.push_back(&a); }

  void addAtom(const Atom &atom) {
    if (auto *p = dyn_cast<DefinedAtom>(&atom)) {
      _defined._atoms.push_back(p);
    } else if (auto *p = dyn_cast<UndefinedAtom>(&atom)) {
      _undefined._atoms.push_back(p);
    } else if (auto *p = dyn_cast<SharedLibraryAtom>(&atom)) {
      _shared._atoms.push_back(p);
    } else if (auto *p = dyn_cast<AbsoluteAtom>(&atom)) {
      _absolute._atoms.push_back(p);
    } else {
      llvm_unreachable("atom has unknown definition kind");
    }
  }

  void removeDefinedAtomsIf(std::function<bool(const DefinedAtom *)> pred) {
    auto &atoms = _defined._atoms;
    auto newEnd = std::remove_if(atoms.begin(), atoms.end(), pred);
    atoms.erase(newEnd, atoms.end());
  }

  const atom_collection<DefinedAtom> &defined() const override {
    return _defined;
  }

  const atom_collection<UndefinedAtom> &undefined() const override {
    return _undefined;
  }

  const atom_collection<SharedLibraryAtom> &sharedLibrary() const override {
    return _shared;
  }

  const atom_collection<AbsoluteAtom> &absolute() const override {
    return _absolute;
  }

  typedef range<std::vector<const DefinedAtom *>::iterator> DefinedAtomRange;
  DefinedAtomRange definedAtoms() { return make_range(_defined._atoms); }

private:
  atom_collection_vector<DefinedAtom> _defined;
  atom_collection_vector<UndefinedAtom> _undefined;
  atom_collection_vector<SharedLibraryAtom> _shared;
  atom_collection_vector<AbsoluteAtom> _absolute;
};

/// \brief Archive library file that may be used as a virtual container
/// for symbols that should be added dynamically in response to
/// call to find() method.
class SimpleArchiveLibraryFile : public ArchiveLibraryFile {
public:
  SimpleArchiveLibraryFile(StringRef filename)
      : ArchiveLibraryFile(filename) {}

  const atom_collection<DefinedAtom> &defined() const override {
    return _definedAtoms;
  }

  const atom_collection<UndefinedAtom> &undefined() const override {
    return _undefinedAtoms;
  }

  const atom_collection<SharedLibraryAtom> &sharedLibrary() const override {
    return _sharedLibraryAtoms;
  }

  const atom_collection<AbsoluteAtom> &absolute() const override {
    return _absoluteAtoms;
  }

  File *find(StringRef sym, bool dataSymbolOnly) override {
    // For descendants:
    // do some checks here and return dynamically generated files with atoms.
    return nullptr;
  }
{'fraction_non_alphanumeric': 0.15216787103946636, 'fraction_numerical': 0.0, 'mean_word_length': 3.396456933414783, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1519896', 'n_tokens_mistral': 2245, 'n_tokens_neox': 2191, 'n_words': 511}