xref: /llvm-project/clang-tools-extra/clang-include-fixer/find-all-symbols/PathConfig.cpp (revision 2b00d449d2ed09cd0364038115f90a3eb4fd15b5)
143356f56SNico Weber //===-- PathConfig.cpp - Process paths of symbols ---------------*- C++ -*-===//
243356f56SNico Weber //
343356f56SNico Weber //
443356f56SNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
543356f56SNico Weber // See https://llvm.org/LICENSE.txt for license information.
643356f56SNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
743356f56SNico Weber //
843356f56SNico Weber //===----------------------------------------------------------------------===//
943356f56SNico Weber 
1043356f56SNico Weber #include "PathConfig.h"
1176221c73SReid Kleckner #include "llvm/ADT/SmallString.h"
1243356f56SNico Weber #include "llvm/Support/Path.h"
1343356f56SNico Weber 
1443356f56SNico Weber namespace clang {
1543356f56SNico Weber namespace find_all_symbols {
1643356f56SNico Weber 
getIncludePath(const SourceManager & SM,SourceLocation Loc,const HeaderMapCollector * Collector)1743356f56SNico Weber std::string getIncludePath(const SourceManager &SM, SourceLocation Loc,
1843356f56SNico Weber                            const HeaderMapCollector *Collector) {
1943356f56SNico Weber   llvm::StringRef FilePath;
2043356f56SNico Weber   // Walk up the include stack to skip .inc files.
2143356f56SNico Weber   while (true) {
2243356f56SNico Weber     if (!Loc.isValid() || SM.isInMainFile(Loc))
2343356f56SNico Weber       return "";
2443356f56SNico Weber     FilePath = SM.getFilename(Loc);
2543356f56SNico Weber     if (FilePath.empty())
2643356f56SNico Weber       return "";
27732bccb8SKazu Hirata     if (!FilePath.ends_with(".inc"))
2843356f56SNico Weber       break;
2943356f56SNico Weber     FileID ID = SM.getFileID(Loc);
3043356f56SNico Weber     Loc = SM.getIncludeLoc(ID);
3143356f56SNico Weber   }
3243356f56SNico Weber 
3343356f56SNico Weber   if (Collector)
3443356f56SNico Weber     FilePath = Collector->getMappedHeader(FilePath);
3543356f56SNico Weber   SmallString<256> CleanedFilePath = FilePath;
3643356f56SNico Weber   llvm::sys::path::remove_dots(CleanedFilePath, /*remove_dot_dot=*/false);
3743356f56SNico Weber 
38*2b00d449SKazu Hirata   return std::string(CleanedFilePath);
3943356f56SNico Weber }
4043356f56SNico Weber 
4143356f56SNico Weber } // namespace find_all_symbols
4243356f56SNico Weber } // namespace clang
43