[clangd] Add isHeaderFile helper.
Summary:
we have a few places using `ASTCtx.getLangOpts().IsHeaderFile` to
determine a header file, but it relies on "-x c-header" compiler flag,
if the compilation command doesn't have this flag, we will get a false
positive. We are encountering this issue in bazel build system.
To solve this problem, we infer the file from file name, actual changes will
come in follow-ups.
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D70235
diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp
index d505645..b36d11d 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -17,6 +17,7 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TokenKinds.h"
+#include "clang/Driver/Types.h"
#include "clang/Format/Format.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
@@ -1122,5 +1123,17 @@
return ER;
}
+bool isHeaderFile(llvm::StringRef FileName,
+ llvm::Optional<LangOptions> LangOpts) {
+ // Respect the langOpts, for non-file-extension cases, e.g. standard library
+ // files.
+ if (LangOpts && LangOpts->IsHeaderFile)
+ return true;
+ namespace types = clang::driver::types;
+ auto Lang = types::lookupTypeForExtension(
+ llvm::sys::path::extension(FileName).substr(1));
+ return Lang != types::TY_INVALID && types::onlyPrecompileType(Lang);
+}
+
} // namespace clangd
} // namespace clang