1 //===--- FileExtensionsUtils.cpp - 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 #include "FileExtensionsUtils.h" 10 #include "clang/Basic/CharInfo.h" 11 #include "llvm/Support/Path.h" 12 13 namespace clang { 14 namespace tidy { 15 namespace utils { 16 17 bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, 18 const FileExtensionsSet &HeaderFileExtensions) { 19 SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); 20 return isFileExtension(SM.getFilename(ExpansionLoc), HeaderFileExtensions); 21 } 22 23 bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, 24 const FileExtensionsSet &HeaderFileExtensions) { 25 PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc); 26 return isFileExtension(PresumedLocation.getFilename(), HeaderFileExtensions); 27 } 28 29 bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, 30 const FileExtensionsSet &HeaderFileExtensions) { 31 SourceLocation SpellingLoc = SM.getSpellingLoc(Loc); 32 return isFileExtension(SM.getFilename(SpellingLoc), HeaderFileExtensions); 33 } 34 35 bool parseFileExtensions(StringRef AllFileExtensions, 36 FileExtensionsSet &FileExtensions, 37 StringRef Delimiters) { 38 SmallVector<StringRef, 5> Suffixes; 39 for (char Delimiter : Delimiters) { 40 if (AllFileExtensions.contains(Delimiter)) { 41 AllFileExtensions.split(Suffixes, Delimiter); 42 break; 43 } 44 } 45 46 FileExtensions.clear(); 47 for (StringRef Suffix : Suffixes) { 48 StringRef Extension = Suffix.trim(); 49 if (!llvm::all_of(Extension, isAlphanumeric)) 50 return false; 51 FileExtensions.insert(Extension); 52 } 53 return true; 54 } 55 56 bool isFileExtension(StringRef FileName, 57 const FileExtensionsSet &FileExtensions) { 58 StringRef Extension = llvm::sys::path::extension(FileName); 59 if (Extension.empty()) 60 return false; 61 // Skip "." prefix. 62 return FileExtensions.count(Extension.substr(1)) > 0; 63 } 64 65 } // namespace utils 66 } // namespace tidy 67 } // namespace clang 68