• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- HeaderGuard.cpp - clang-tidy -------------------------------------===//
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 "HeaderGuard.h"
10 #include "clang/Frontend/CompilerInstance.h"
11 #include "clang/Lex/PPCallbacks.h"
12 #include "clang/Lex/Preprocessor.h"
13 #include "clang/Tooling/Tooling.h"
14 #include "llvm/Support/Path.h"
15 
16 namespace clang {
17 namespace tidy {
18 namespace utils {
19 
20 /// canonicalize a path by removing ./ and ../ components.
cleanPath(StringRef Path)21 static std::string cleanPath(StringRef Path) {
22   SmallString<256> Result = Path;
23   llvm::sys::path::remove_dots(Result, true);
24   return std::string(Result.str());
25 }
26 
27 namespace {
28 class HeaderGuardPPCallbacks : public PPCallbacks {
29 public:
HeaderGuardPPCallbacks(Preprocessor * PP,HeaderGuardCheck * Check)30   HeaderGuardPPCallbacks(Preprocessor *PP, HeaderGuardCheck *Check)
31       : PP(PP), Check(Check) {}
32 
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind FileType,FileID PrevFID)33   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
34                    SrcMgr::CharacteristicKind FileType,
35                    FileID PrevFID) override {
36     // Record all files we enter. We'll need them to diagnose headers without
37     // guards.
38     SourceManager &SM = PP->getSourceManager();
39     if (Reason == EnterFile && FileType == SrcMgr::C_User) {
40       if (const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc))) {
41         std::string FileName = cleanPath(FE->getName());
42         Files[FileName] = FE;
43       }
44     }
45   }
46 
Ifndef(SourceLocation Loc,const Token & MacroNameTok,const MacroDefinition & MD)47   void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
48               const MacroDefinition &MD) override {
49     if (MD)
50       return;
51 
52     // Record #ifndefs that succeeded. We also need the Location of the Name.
53     Ifndefs[MacroNameTok.getIdentifierInfo()] =
54         std::make_pair(Loc, MacroNameTok.getLocation());
55   }
56 
MacroDefined(const Token & MacroNameTok,const MacroDirective * MD)57   void MacroDefined(const Token &MacroNameTok,
58                     const MacroDirective *MD) override {
59     // Record all defined macros. We store the whole token to get info on the
60     // name later.
61     Macros.emplace_back(MacroNameTok, MD->getMacroInfo());
62   }
63 
Endif(SourceLocation Loc,SourceLocation IfLoc)64   void Endif(SourceLocation Loc, SourceLocation IfLoc) override {
65     // Record all #endif and the corresponding #ifs (including #ifndefs).
66     EndIfs[IfLoc] = Loc;
67   }
68 
EndOfMainFile()69   void EndOfMainFile() override {
70     // Now that we have all this information from the preprocessor, use it!
71     SourceManager &SM = PP->getSourceManager();
72 
73     for (const auto &MacroEntry : Macros) {
74       const MacroInfo *MI = MacroEntry.second;
75 
76       // We use clang's header guard detection. This has the advantage of also
77       // emitting a warning for cases where a pseudo header guard is found but
78       // preceded by something blocking the header guard optimization.
79       if (!MI->isUsedForHeaderGuard())
80         continue;
81 
82       const FileEntry *FE =
83           SM.getFileEntryForID(SM.getFileID(MI->getDefinitionLoc()));
84       std::string FileName = cleanPath(FE->getName());
85       Files.erase(FileName);
86 
87       // See if we should check and fix this header guard.
88       if (!Check->shouldFixHeaderGuard(FileName))
89         continue;
90 
91       // Look up Locations for this guard.
92       SourceLocation Ifndef =
93           Ifndefs[MacroEntry.first.getIdentifierInfo()].second;
94       SourceLocation Define = MacroEntry.first.getLocation();
95       SourceLocation EndIf =
96           EndIfs[Ifndefs[MacroEntry.first.getIdentifierInfo()].first];
97 
98       // If the macro Name is not equal to what we can compute, correct it in
99       // the #ifndef and #define.
100       StringRef CurHeaderGuard =
101           MacroEntry.first.getIdentifierInfo()->getName();
102       std::vector<FixItHint> FixIts;
103       std::string NewGuard = checkHeaderGuardDefinition(
104           Ifndef, Define, EndIf, FileName, CurHeaderGuard, FixIts);
105 
106       // Now look at the #endif. We want a comment with the header guard. Fix it
107       // at the slightest deviation.
108       checkEndifComment(FileName, EndIf, NewGuard, FixIts);
109 
110       // Bundle all fix-its into one warning. The message depends on whether we
111       // changed the header guard or not.
112       if (!FixIts.empty()) {
113         if (CurHeaderGuard != NewGuard) {
114           Check->diag(Ifndef, "header guard does not follow preferred style")
115               << FixIts;
116         } else {
117           Check->diag(EndIf, "#endif for a header guard should reference the "
118                              "guard macro in a comment")
119               << FixIts;
120         }
121       }
122     }
123 
124     // Emit warnings for headers that are missing guards.
125     checkGuardlessHeaders();
126     clearAllState();
127   }
128 
wouldFixEndifComment(StringRef FileName,SourceLocation EndIf,StringRef HeaderGuard,size_t * EndIfLenPtr=nullptr)129   bool wouldFixEndifComment(StringRef FileName, SourceLocation EndIf,
130                             StringRef HeaderGuard,
131                             size_t *EndIfLenPtr = nullptr) {
132     if (!EndIf.isValid())
133       return false;
134     const char *EndIfData = PP->getSourceManager().getCharacterData(EndIf);
135     size_t EndIfLen = std::strcspn(EndIfData, "\r\n");
136     if (EndIfLenPtr)
137       *EndIfLenPtr = EndIfLen;
138 
139     StringRef EndIfStr(EndIfData, EndIfLen);
140     EndIfStr = EndIfStr.substr(EndIfStr.find_first_not_of("#endif \t"));
141 
142     // Give up if there's an escaped newline.
143     size_t FindEscapedNewline = EndIfStr.find_last_not_of(' ');
144     if (FindEscapedNewline != StringRef::npos &&
145         EndIfStr[FindEscapedNewline] == '\\')
146       return false;
147 
148     if (!Check->shouldSuggestEndifComment(FileName) &&
149         !(EndIfStr.startswith("//") ||
150           (EndIfStr.startswith("/*") && EndIfStr.endswith("*/"))))
151       return false;
152 
153     return (EndIfStr != "// " + HeaderGuard.str()) &&
154            (EndIfStr != "/* " + HeaderGuard.str() + " */");
155   }
156 
157   /// Look for header guards that don't match the preferred style. Emit
158   /// fix-its and return the suggested header guard (or the original if no
159   /// change was made.
checkHeaderGuardDefinition(SourceLocation Ifndef,SourceLocation Define,SourceLocation EndIf,StringRef FileName,StringRef CurHeaderGuard,std::vector<FixItHint> & FixIts)160   std::string checkHeaderGuardDefinition(SourceLocation Ifndef,
161                                          SourceLocation Define,
162                                          SourceLocation EndIf,
163                                          StringRef FileName,
164                                          StringRef CurHeaderGuard,
165                                          std::vector<FixItHint> &FixIts) {
166     std::string CPPVar = Check->getHeaderGuard(FileName, CurHeaderGuard);
167     std::string CPPVarUnder = CPPVar + '_';
168 
169     // Allow a trailing underscore iff we don't have to change the endif comment
170     // too.
171     if (Ifndef.isValid() && CurHeaderGuard != CPPVar &&
172         (CurHeaderGuard != CPPVarUnder ||
173          wouldFixEndifComment(FileName, EndIf, CurHeaderGuard))) {
174       FixIts.push_back(FixItHint::CreateReplacement(
175           CharSourceRange::getTokenRange(
176               Ifndef, Ifndef.getLocWithOffset(CurHeaderGuard.size())),
177           CPPVar));
178       FixIts.push_back(FixItHint::CreateReplacement(
179           CharSourceRange::getTokenRange(
180               Define, Define.getLocWithOffset(CurHeaderGuard.size())),
181           CPPVar));
182       return CPPVar;
183     }
184     return std::string(CurHeaderGuard);
185   }
186 
187   /// Checks the comment after the #endif of a header guard and fixes it
188   /// if it doesn't match \c HeaderGuard.
checkEndifComment(StringRef FileName,SourceLocation EndIf,StringRef HeaderGuard,std::vector<FixItHint> & FixIts)189   void checkEndifComment(StringRef FileName, SourceLocation EndIf,
190                          StringRef HeaderGuard,
191                          std::vector<FixItHint> &FixIts) {
192     size_t EndIfLen;
193     if (wouldFixEndifComment(FileName, EndIf, HeaderGuard, &EndIfLen)) {
194       FixIts.push_back(FixItHint::CreateReplacement(
195           CharSourceRange::getCharRange(EndIf,
196                                         EndIf.getLocWithOffset(EndIfLen)),
197           Check->formatEndIf(HeaderGuard)));
198     }
199   }
200 
201   /// Looks for files that were visited but didn't have a header guard.
202   /// Emits a warning with fixits suggesting adding one.
checkGuardlessHeaders()203   void checkGuardlessHeaders() {
204     // Look for header files that didn't have a header guard. Emit a warning and
205     // fix-its to add the guard.
206     // TODO: Insert the guard after top comments.
207     for (const auto &FE : Files) {
208       StringRef FileName = FE.getKey();
209       if (!Check->shouldSuggestToAddHeaderGuard(FileName))
210         continue;
211 
212       SourceManager &SM = PP->getSourceManager();
213       FileID FID = SM.translateFile(FE.getValue());
214       SourceLocation StartLoc = SM.getLocForStartOfFile(FID);
215       if (StartLoc.isInvalid())
216         continue;
217 
218       std::string CPPVar = Check->getHeaderGuard(FileName);
219       std::string CPPVarUnder = CPPVar + '_'; // Allow a trailing underscore.
220       // If there's a macro with a name that follows the header guard convention
221       // but was not recognized by the preprocessor as a header guard there must
222       // be code outside of the guarded area. Emit a plain warning without
223       // fix-its.
224       // FIXME: Can we move it into the right spot?
225       bool SeenMacro = false;
226       for (const auto &MacroEntry : Macros) {
227         StringRef Name = MacroEntry.first.getIdentifierInfo()->getName();
228         SourceLocation DefineLoc = MacroEntry.first.getLocation();
229         if ((Name == CPPVar || Name == CPPVarUnder) &&
230             SM.isWrittenInSameFile(StartLoc, DefineLoc)) {
231           Check->diag(DefineLoc, "code/includes outside of area guarded by "
232                                  "header guard; consider moving it");
233           SeenMacro = true;
234           break;
235         }
236       }
237 
238       if (SeenMacro)
239         continue;
240 
241       Check->diag(StartLoc, "header is missing header guard")
242           << FixItHint::CreateInsertion(
243                  StartLoc, "#ifndef " + CPPVar + "\n#define " + CPPVar + "\n\n")
244           << FixItHint::CreateInsertion(
245                  SM.getLocForEndOfFile(FID),
246                  Check->shouldSuggestEndifComment(FileName)
247                      ? "\n#" + Check->formatEndIf(CPPVar) + "\n"
248                      : "\n#endif\n");
249     }
250   }
251 
252 private:
clearAllState()253   void clearAllState() {
254     Macros.clear();
255     Files.clear();
256     Ifndefs.clear();
257     EndIfs.clear();
258   }
259 
260   std::vector<std::pair<Token, const MacroInfo *>> Macros;
261   llvm::StringMap<const FileEntry *> Files;
262   std::map<const IdentifierInfo *, std::pair<SourceLocation, SourceLocation>>
263       Ifndefs;
264   std::map<SourceLocation, SourceLocation> EndIfs;
265 
266   Preprocessor *PP;
267   HeaderGuardCheck *Check;
268 };
269 } // namespace
270 
storeOptions(ClangTidyOptions::OptionMap & Opts)271 void HeaderGuardCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
272   Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
273 }
274 
registerPPCallbacks(const SourceManager & SM,Preprocessor * PP,Preprocessor * ModuleExpanderPP)275 void HeaderGuardCheck::registerPPCallbacks(const SourceManager &SM,
276                                            Preprocessor *PP,
277                                            Preprocessor *ModuleExpanderPP) {
278   PP->addPPCallbacks(std::make_unique<HeaderGuardPPCallbacks>(PP, this));
279 }
280 
shouldSuggestEndifComment(StringRef FileName)281 bool HeaderGuardCheck::shouldSuggestEndifComment(StringRef FileName) {
282   return utils::isFileExtension(FileName, HeaderFileExtensions);
283 }
284 
shouldFixHeaderGuard(StringRef FileName)285 bool HeaderGuardCheck::shouldFixHeaderGuard(StringRef FileName) { return true; }
286 
shouldSuggestToAddHeaderGuard(StringRef FileName)287 bool HeaderGuardCheck::shouldSuggestToAddHeaderGuard(StringRef FileName) {
288   return utils::isFileExtension(FileName, HeaderFileExtensions);
289 }
290 
formatEndIf(StringRef HeaderGuard)291 std::string HeaderGuardCheck::formatEndIf(StringRef HeaderGuard) {
292   return "endif // " + HeaderGuard.str();
293 }
294 } // namespace utils
295 } // namespace tidy
296 } // namespace clang
297