1 //===--- FileExtensionsUtils.h - clang-tidy --------------------*- 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_FILE_EXTENSIONS_UTILS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_FILE_EXTENSIONS_UTILS_H
11 
12 #include "../FileExtensionsSet.h"
13 #include "clang/Basic/SourceLocation.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "llvm/ADT/SmallSet.h"
16 #include "llvm/ADT/StringRef.h"
17 #include <optional>
18 
19 namespace clang::tidy::utils {
20 
21 /// Checks whether expansion location of \p Loc is in header file.
22 bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM,
23                                 const FileExtensionsSet &HeaderFileExtensions);
24 
25 /// Checks whether presumed location of \p Loc is in header file.
26 bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM,
27                                const FileExtensionsSet &HeaderFileExtensions);
28 
29 /// Checks whether spelling location of \p Loc is in header file.
30 bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM,
31                                const FileExtensionsSet &HeaderFileExtensions);
32 
33 /// Returns recommended default value for the list of header file
34 /// extensions.
defaultHeaderFileExtensions()35 inline StringRef defaultHeaderFileExtensions() { return ";h;hh;hpp;hxx"; }
36 
37 /// Returns recommended default value for the list of implementation file
38 /// extensions.
defaultImplementationFileExtensions()39 inline StringRef defaultImplementationFileExtensions() {
40   return "c;cc;cpp;cxx";
41 }
42 
43 /// Returns recommended default value for the list of file extension
44 /// delimiters.
defaultFileExtensionDelimiters()45 inline StringRef defaultFileExtensionDelimiters() { return ",;"; }
46 
47 /// Parses header file extensions from a semicolon-separated list.
48 bool parseFileExtensions(StringRef AllFileExtensions,
49                          FileExtensionsSet &FileExtensions,
50                          StringRef Delimiters);
51 
52 /// Decides whether a file has a header file extension.
53 /// Returns the file extension, if included in the provided set.
54 std::optional<StringRef>
55 getFileExtension(StringRef FileName, const FileExtensionsSet &FileExtensions);
56 
57 /// Decides whether a file has one of the specified file extensions.
58 bool isFileExtension(StringRef FileName,
59                      const FileExtensionsSet &FileExtensions);
60 
61 } // namespace clang::tidy::utils
62 
63 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_FILE_EXTENSIONS_UTILS_H
64