llvm-project/clang/tools/libclang/BuildSystem.cpp
serge-sans-paille 75e164f61d [llvm] Cleanup header dependencies in ADT and Support
The cleanup was manual, but assisted by "include-what-you-use". It consists in

1. Removing unused forward declaration. No impact expected.
2. Removing unused headers in .cpp files. No impact expected.
3. Removing unused headers in .h files. This removes implicit dependencies and
   is generally considered a good thing, but this may break downstream builds.
   I've updated llvm, clang, lld, lldb and mlir deps, and included a list of the
   modification in the second part of the commit.
4. Replacing header inclusion by forward declaration. This has the same impact
   as 3.

Notable changes:

- llvm/Support/TargetParser.h no longer includes llvm/Support/AArch64TargetParser.h nor llvm/Support/ARMTargetParser.h
- llvm/Support/TypeSize.h no longer includes llvm/Support/WithColor.h
- llvm/Support/YAMLTraits.h no longer includes llvm/Support/Regex.h
- llvm/ADT/SmallVector.h no longer includes llvm/Support/MemAlloc.h nor llvm/Support/ErrorHandling.h

You may need to add some of these headers in your compilation units, if needs be.

As an hint to the impact of the cleanup, running

clang++ -E  -Iinclude -I../llvm/include ../llvm/lib/Support/*.cpp -std=c++14 -fno-rtti -fno-exceptions | wc -l

before: 8000919 lines
after:  7917500 lines

Reduced dependencies also helps incremental rebuilds and is more ccache
friendly, something not shown by the above metric :-)

Discourse thread on the topic: https://llvm.discourse.group/t/include-what-you-use-include-cleanup/5831
2022-01-21 13:54:49 +01:00

153 lines
4.6 KiB
C++

//===- BuildSystem.cpp - Utilities for use by build systems ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements various utilities for use by build systems.
//
//===----------------------------------------------------------------------===//
#include "clang-c/BuildSystem.h"
#include "CXString.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemAlloc.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace llvm::sys;
unsigned long long clang_getBuildSessionTimestamp(void) {
return llvm::sys::toTimeT(std::chrono::system_clock::now());
}
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::vfs::YAMLVFSWriter,
CXVirtualFileOverlay)
CXVirtualFileOverlay clang_VirtualFileOverlay_create(unsigned) {
return wrap(new llvm::vfs::YAMLVFSWriter());
}
enum CXErrorCode
clang_VirtualFileOverlay_addFileMapping(CXVirtualFileOverlay VFO,
const char *virtualPath,
const char *realPath) {
if (!VFO || !virtualPath || !realPath)
return CXError_InvalidArguments;
if (!path::is_absolute(virtualPath))
return CXError_InvalidArguments;
if (!path::is_absolute(realPath))
return CXError_InvalidArguments;
for (path::const_iterator
PI = path::begin(virtualPath),
PE = path::end(virtualPath); PI != PE; ++PI) {
StringRef Comp = *PI;
if (Comp == "." || Comp == "..")
return CXError_InvalidArguments;
}
unwrap(VFO)->addFileMapping(virtualPath, realPath);
return CXError_Success;
}
enum CXErrorCode
clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO,
int caseSensitive) {
if (!VFO)
return CXError_InvalidArguments;
unwrap(VFO)->setCaseSensitivity(caseSensitive);
return CXError_Success;
}
enum CXErrorCode
clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO, unsigned,
char **out_buffer_ptr,
unsigned *out_buffer_size) {
if (!VFO || !out_buffer_ptr || !out_buffer_size)
return CXError_InvalidArguments;
llvm::SmallString<256> Buf;
llvm::raw_svector_ostream OS(Buf);
unwrap(VFO)->write(OS);
StringRef Data = OS.str();
*out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
*out_buffer_size = Data.size();
memcpy(*out_buffer_ptr, Data.data(), Data.size());
return CXError_Success;
}
void clang_free(void *buffer) {
free(buffer);
}
void clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO) {
delete unwrap(VFO);
}
struct CXModuleMapDescriptorImpl {
std::string ModuleName;
std::string UmbrellaHeader;
};
CXModuleMapDescriptor clang_ModuleMapDescriptor_create(unsigned) {
return new CXModuleMapDescriptorImpl();
}
enum CXErrorCode
clang_ModuleMapDescriptor_setFrameworkModuleName(CXModuleMapDescriptor MMD,
const char *name) {
if (!MMD || !name)
return CXError_InvalidArguments;
MMD->ModuleName = name;
return CXError_Success;
}
enum CXErrorCode
clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor MMD,
const char *name) {
if (!MMD || !name)
return CXError_InvalidArguments;
MMD->UmbrellaHeader = name;
return CXError_Success;
}
enum CXErrorCode
clang_ModuleMapDescriptor_writeToBuffer(CXModuleMapDescriptor MMD, unsigned,
char **out_buffer_ptr,
unsigned *out_buffer_size) {
if (!MMD || !out_buffer_ptr || !out_buffer_size)
return CXError_InvalidArguments;
llvm::SmallString<256> Buf;
llvm::raw_svector_ostream OS(Buf);
OS << "framework module " << MMD->ModuleName << " {\n";
OS << " umbrella header \"";
OS.write_escaped(MMD->UmbrellaHeader) << "\"\n";
OS << '\n';
OS << " export *\n";
OS << " module * { export * }\n";
OS << "}\n";
StringRef Data = OS.str();
*out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
*out_buffer_size = Data.size();
memcpy(*out_buffer_ptr, Data.data(), Data.size());
return CXError_Success;
}
void clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD) {
delete MMD;
}