bump product version to 5.0.4.1
[LibreOffice.git] / compilerplugins / clang / compat.hxx
blob08787e8c77fd6aece4a79ed82bd4d83eee3decaa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef INCLUDED_COMPILERPLUGINS_CLANG_COMPAT_HXX
11 #define INCLUDED_COMPILERPLUGINS_CLANG_COMPAT_HXX
13 #include <memory>
14 #include <string>
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclBase.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/Diagnostic.h"
22 #include "clang/Basic/DiagnosticIDs.h"
23 #include "clang/Basic/Linkage.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "clang/Basic/Visibility.h"
26 #include "clang/Frontend/CompilerInstance.h"
27 #include "clang/Lex/PPCallbacks.h"
28 #include "clang/Lex/Preprocessor.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/raw_ostream.h"
33 #if (__clang_major__ == 3 && __clang_minor__ >= 4) || __clang_major__ > 3
34 #define LO_COMPILERPLUGINS_CLANG_COMPAT_HAVE_isAtEndOfImmediateMacroExpansion \
35 true
36 #else
37 #define LO_COMPILERPLUGINS_CLANG_COMPAT_HAVE_isAtEndOfImmediateMacroExpansion \
38 false
39 #endif
41 // Compatibility wrapper to abstract over (trivial) changes in the Clang API:
42 namespace compat {
44 inline bool isExternCContext(clang::DeclContext const & ctxt) {
45 #if (__clang_major__ == 3 && __clang_minor__ >= 4) || __clang_major__ > 3
46 return ctxt.isExternCContext();
47 #else
48 for (clang::DeclContext const * c = &ctxt;
49 c->getDeclKind() != clang::Decl::TranslationUnit; c = c->getParent())
51 if (c->getDeclKind() == clang::Decl::LinkageSpec) {
52 return llvm::cast<clang::LinkageSpecDecl>(c)->getLanguage()
53 == clang::LinkageSpecDecl::lang_c;
56 return false;
57 #endif
60 inline bool isInExternCContext(clang::FunctionDecl const & decl) {
61 #if (__clang_major__ == 3 && __clang_minor__ >= 4) || __clang_major__ > 3
62 return decl.isInExternCContext();
63 #else
64 return isExternCContext(*decl.getCanonicalDecl()->getDeclContext());
65 #endif
68 #if (__clang_major__ == 3 && __clang_minor__ >= 3) || __clang_major__ > 3
69 typedef clang::LinkageInfo LinkageInfo;
70 #else
71 typedef clang::NamedDecl::LinkageInfo LinkageInfo;
72 #endif
74 inline clang::Linkage getLinkage(LinkageInfo const & info) {
75 #if (__clang_major__ == 3 && __clang_minor__ >= 3) || __clang_major__ > 3
76 return info.getLinkage();
77 #else
78 return info.linkage();
79 #endif
82 inline clang::Visibility getVisibility(LinkageInfo const & info) {
83 #if (__clang_major__ == 3 && __clang_minor__ >= 3) || __clang_major__ > 3
84 return info.getVisibility();
85 #else
86 return info.visibility();
87 #endif
90 inline bool isFirstDecl(clang::FunctionDecl const & decl) {
91 #if (__clang_major__ == 3 && __clang_minor__ >= 4) || __clang_major__ > 3
92 return decl.isFirstDecl();
93 #else
94 return decl.isFirstDeclaration();
95 #endif
98 inline clang::QualType getReturnType(clang::FunctionDecl const & decl) {
99 #if (__clang_major__ == 3 && __clang_minor__ >= 5) || __clang_major__ > 3
100 return decl.getReturnType();
101 #else
102 return decl.getResultType();
103 #endif
106 inline clang::QualType getReturnType(clang::FunctionProtoType const & type) {
107 #if (__clang_major__ == 3 && __clang_minor__ >= 5) || __clang_major__ > 3
108 return type.getReturnType();
109 #else
110 return type.getResultType();
111 #endif
114 inline unsigned getNumParams(clang::FunctionProtoType const & type) {
115 #if (__clang_major__ == 3 && __clang_minor__ >= 5) || __clang_major__ > 3
116 return type.getNumParams();
117 #else
118 return type.getNumArgs();
119 #endif
122 inline clang::QualType getParamType(
123 clang::FunctionProtoType const & type, unsigned i)
125 #if (__clang_major__ == 3 && __clang_minor__ >= 5) || __clang_major__ > 3
126 return type.getParamType(i);
127 #else
128 return type.getArgType(i);
129 #endif
132 inline unsigned getBuiltinCallee(clang::CallExpr const & expr) {
133 #if (__clang_major__ == 3 && __clang_minor__ >= 5) || __clang_major__ > 3
134 return expr.getBuiltinCallee();
135 #else
136 return expr.isBuiltinCall();
137 #endif
140 inline bool isInMainFile(
141 clang::SourceManager const & manager, clang::SourceLocation Loc)
143 #if (__clang_major__ == 3 && __clang_minor__ >= 4) || __clang_major__ > 3
144 return manager.isInMainFile(Loc);
145 #else
146 return manager.isFromMainFile(Loc);
147 #endif
150 inline unsigned getCustomDiagID(
151 clang::DiagnosticsEngine & engine, clang::DiagnosticsEngine::Level L,
152 llvm::StringRef FormatString)
154 #if (__clang_major__ == 3 && __clang_minor__ >= 5) || __clang_major__ > 3
155 return engine.getDiagnosticIDs()->getCustomDiagID(
156 static_cast<clang::DiagnosticIDs::Level>(L), FormatString);
157 #else
158 return engine.getCustomDiagID(L, FormatString);
159 #endif
162 inline std::unique_ptr<llvm::raw_fd_ostream> create_raw_fd_ostream(
163 char const * Filename, std::string & ErrorInfo)
165 #if (__clang_major__ == 3 && __clang_minor__ >= 6) || __clang_major__ > 3
166 std::error_code ec;
167 std::unique_ptr<llvm::raw_fd_ostream> s(
168 new llvm::raw_fd_ostream(Filename, ec, llvm::sys::fs::F_None));
169 ErrorInfo = ec ? "error: " + ec.message() : std::string();
170 return s;
171 #elif __clang_major__ == 3 && __clang_minor__ == 5
172 return std::unique_ptr<llvm::raw_fd_ostream>(
173 new llvm::raw_fd_ostream(Filename, ErrorInfo, llvm::sys::fs::F_None));
174 #else
175 return std::unique_ptr<llvm::raw_fd_ostream>(
176 new llvm::raw_fd_ostream(Filename, ErrorInfo));
177 #endif
180 #if (__clang_major__ == 3 && __clang_minor__ >= 7) || __clang_major__ > 3
181 typedef clang::DeclContext::lookup_result DeclContextLookupResult;
182 typedef clang::DeclContext::lookup_iterator DeclContextLookupIterator;
183 #else
184 typedef clang::DeclContext::lookup_const_result DeclContextLookupResult;
185 typedef clang::DeclContext::lookup_const_iterator DeclContextLookupIterator;
186 #endif
188 inline DeclContextLookupIterator begin(DeclContextLookupResult const & result) {
189 #if (__clang_major__ == 3 && __clang_minor__ >= 3) || __clang_major__ > 3
190 return result.begin();
191 #else
192 return result.first;
193 #endif
196 inline DeclContextLookupIterator end(DeclContextLookupResult const & result) {
197 #if (__clang_major__ == 3 && __clang_minor__ >= 3) || __clang_major__ > 3
198 return result.end();
199 #else
200 return result.second;
201 #endif
204 inline void addPPCallbacks(
205 clang::Preprocessor & preprocessor, clang::PPCallbacks * C)
207 #if (__clang_major__ == 3 && __clang_minor__ >= 6) || __clang_major__ > 3
208 preprocessor.addPPCallbacks(std::unique_ptr<clang::PPCallbacks>(C));
209 #else
210 preprocessor.addPPCallbacks(C);
211 #endif
214 inline bool isMacroBodyExpansion(clang::CompilerInstance& compiler, clang::SourceLocation location)
216 #if (__clang_major__ == 3 && __clang_minor__ >= 3) || __clang_major__ > 3
217 return compiler.getSourceManager().isMacroBodyExpansion(location);
218 #else
219 return location.isMacroID()
220 && !compiler.getSourceManager().isMacroArgExpansion(location);
221 #endif
226 #endif
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */