Skip to content

Commit acc3266

Browse files
authored
[Clang][NFC] Refactor Targets.h to make it publicly accessible (#116090)
This PR is motivated by the requirements of ClangIR, which includes compilation pipelines that do not always start from the Clang driver. In these cases, accessing some target-specific information, such as obtaining a data layout string for a given target triple or querying other target details, requires foundational infrastructure like `clang::TargetInfo`. Since ClangIR is actively being upstreamed, sharing this logic across components has become essential, which leads to this PR. The function `clang::targets::AllocateTarget` serves as the factory for Clang's `TargetInfo`. To enable sharing, this PR moves `AllocateTarget` to a public header. The existing header `clang/lib/Basic/Targets.h` previously contained two parts: the `AllocateTarget` function and target-specific macro helpers. With `AllocateTarget` moved, only the macro stuff remain in `Targets.h`. To better organize the code, the macro helpers have been relocated to a new file, `clang/lib/Basic/TargetDefines.h` (essentially a rename). The original `Targets.h` now serves as a proxy header that includes both headers to maintain compatibility.
1 parent d2a2236 commit acc3266

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

clang/include/clang/Basic/TargetInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,11 @@ class TargetInfo : public TransferrableTargetInfo,
18711871
void CheckFixedPointBits() const;
18721872
};
18731873

1874+
namespace targets {
1875+
std::unique_ptr<clang::TargetInfo>
1876+
AllocateTarget(const llvm::Triple &Triple, const clang::TargetOptions &Opts);
1877+
} // namespace targets
1878+
18741879
} // end namespace clang
18751880

18761881
#endif

clang/lib/Basic/TargetDefines.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===------- TargetDefines.h - Target define helpers ------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares a series of helper functions for defining target-specific
10+
// macros.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CLANG_LIB_BASIC_TARGETDEFINES_H
15+
#define LLVM_CLANG_LIB_BASIC_TARGETDEFINES_H
16+
17+
#include "clang/Basic/LangOptions.h"
18+
#include "clang/Basic/MacroBuilder.h"
19+
#include "llvm/ADT/StringRef.h"
20+
21+
namespace clang {
22+
namespace targets {
23+
/// Define a macro name and standard variants. For example if MacroName is
24+
/// "unix", then this will define "__unix", "__unix__", and "unix" when in GNU
25+
/// mode.
26+
LLVM_LIBRARY_VISIBILITY
27+
void DefineStd(clang::MacroBuilder &Builder, llvm::StringRef MacroName,
28+
const clang::LangOptions &Opts);
29+
30+
LLVM_LIBRARY_VISIBILITY
31+
void defineCPUMacros(clang::MacroBuilder &Builder, llvm::StringRef CPUName,
32+
bool Tuning = true);
33+
34+
LLVM_LIBRARY_VISIBILITY
35+
void addCygMingDefines(const clang::LangOptions &Opts,
36+
clang::MacroBuilder &Builder);
37+
} // namespace targets
38+
} // namespace clang
39+
#endif // LLVM_CLANG_LIB_BASIC_TARGETDEFINES_H

clang/lib/Basic/Targets.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,7 @@
1515
#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_H
1616
#define LLVM_CLANG_LIB_BASIC_TARGETS_H
1717

18-
#include "clang/Basic/LangOptions.h"
19-
#include "clang/Basic/MacroBuilder.h"
18+
#include "TargetDefines.h"
2019
#include "clang/Basic/TargetInfo.h"
21-
#include "llvm/ADT/StringRef.h"
2220

23-
namespace clang {
24-
namespace targets {
25-
26-
LLVM_LIBRARY_VISIBILITY
27-
std::unique_ptr<clang::TargetInfo>
28-
AllocateTarget(const llvm::Triple &Triple, const clang::TargetOptions &Opts);
29-
30-
/// DefineStd - Define a macro name and standard variants. For example if
31-
/// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
32-
/// when in GNU mode.
33-
LLVM_LIBRARY_VISIBILITY
34-
void DefineStd(clang::MacroBuilder &Builder, llvm::StringRef MacroName,
35-
const clang::LangOptions &Opts);
36-
37-
LLVM_LIBRARY_VISIBILITY
38-
void defineCPUMacros(clang::MacroBuilder &Builder, llvm::StringRef CPUName,
39-
bool Tuning = true);
40-
41-
LLVM_LIBRARY_VISIBILITY
42-
void addCygMingDefines(const clang::LangOptions &Opts,
43-
clang::MacroBuilder &Builder);
44-
} // namespace targets
45-
} // namespace clang
4621
#endif // LLVM_CLANG_LIB_BASIC_TARGETS_H

0 commit comments

Comments
 (0)